UITableViewCell dequeueReusableCellWithIdentifier不按顺序排列

时间:2011-02-26 18:18:28

标签: iphone cocoa-touch uitableview iphone-sdk-3.0 ios4

我有一个UITable,其中包含少量包含文本字段的自定义单元格。然而,当向上和向下滚动时,这些单元格的顺序混乱。

我搜索了这对我不起作用因为我有很少的UITextFields,我需要获取那些用户输入。

UITableViewCell showing indexpath rows out of order

谢谢

3 个答案:

答案 0 :(得分:1)

您需要为每个单元格使用唯一标识符,否则完全避免使用出列机制。如果您正在执行后者,请在相应的实例变量中存储对每个单元格的引用,或者将它们全部添加到存储在实例变量中的集合。

如果您决定采用这种方法,可以采用以下两种方式之一:在Interface Builder中创建单元格(这将非常方便,因为它们已经可以将文本字段嵌套在其中,定位良好等等。 )并使用outlet连接它们,或者以viewDidLoadinit...方法以编程方式创建单元格。

在任何一种情况下,您都不再需要致电dequeueReusableCellWithIdentifier:;相反,你可以只返回你已经创建的单元格实例。

答案 1 :(得分:0)

@Dilshan您可以在代码中执行与此类似的操作,以达到您想要的效果。

.
.
.
if(cell == nil)
{
// create your cell in this block of quotes in the cellForRowAtIndexPath: method to avoid making of the cells again, so that your textfield value remain as it is.
.
.
.
//place all the code in this block only.
}
[cell autorelease];
return cell;
抱歉,我家里没有xcode,所以我不能给你任何特定的代码。

但我认为你明白了。
欢呼声。

答案 2 :(得分:0)

这是我的代码。 CustomTextFieldCell是一个带有UITextField的单元格。和一些信息。但是,滚动时填充的值和这些值的顺序会发生变化。

    static NSString *CellIdentifier = @"CustomTextFieldCell";
    CustomTextFieldCell *cell = (CustomTextFieldCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTextFieldCell" owner:nil options:nil];


        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (CustomTextFieldCell *) currentObject;
                break;
            }
        }
    }