2010-04-22 72 views
0

我有我在界面生成器中创建的自定义UITableViewCell。我成功地将单元排队,但在滚动时,单元似乎开始调用不同的indexPath。在这个例子中,我将当前的indexPath.section和indexPath.row馈送到customCellLabel中。当我上下滚动表格时,一些单元格会改变。数字可以遍布整个地方,但细胞并没有在视觉上跳过。自定义UITableViewCell在滚动时更改indexPath?

如果我注释掉if(cell == nil),那么问题就会消失。

如果我使用标准单元格,问题就会消失。

想法为什么会发生这种情况?

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CalendarEventCell"]; 
    if (cell == nil) { 
     NSLog(@"Creating New Cell !!!!!"); 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CalendarEventCell" owner:self options:nil]; 
     cell = (UITableViewCell *)[nib objectAtIndex:0]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 


    // Set up the cell... 
    [customCellLabel setText:[NSString stringWithFormat:@"%d - %d",indexPath.section, indexPath.row]]; 


    return cell; 
} 
+0

customCellLabel指的是什么? – jamesmoschou 2010-04-22 06:30:41

+0

customCellLabel是我在界面生成器中创建的CalendarEventCell中包含的标签。 – Chris 2010-04-22 14:59:07

回答

2

当您在如果(细胞==零)的条件,你分配一个reuseIdentifier对小区的“CalendarEventCell”细胞?我可以看到你的笔尖的名字是CalendarEventCell,但我猜你也需要设置

cell.reuseIdentifier = @"CalendarEventCell";

如果没有,那么我不知道,如果它可以双端队列正确的细胞。另外,不知道customCellLabel引用了什么。

+0

当您使用笔尖创建可重用单元时,可以在Interface Builder中设置reuseIdentifier。我已将Reuse Identifier设置为与文件名相同。 我相信它是正确的出队单元,因为当我加载表时,我得到了一些NSLogs,当我开始滚动上下滚动时没有额外的NSLogs。 – Chris 2010-04-22 14:58:37

+0

另外,你不能以这种方式设置cell.resuseIdentifier。 reuseIdentifier是只读的。 – Chris 2010-04-22 15:43:50

+0

对不起,我错过了那一个。你是对的。它是一个只读属性,只能在创建要重用的单元格时设置。 – 2010-04-23 03:33:50

相关问题