我有一个UITableView
高度为 180px 的,一次只能显示 5 行,表格视图包含自定义单元格。但是,我有 6 个单元格,这意味着只有在使用滚动时才会显示第六个单元格,当我尝试像这样遍历所有单元格时:
for (int row = 0; row < [self.autoTableView numberOfRowsInSection:0]; row++) {
NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *cell = [self.autoTableView cellForRowAtIndexPath:cellPath];
if(!cell)
NSLog(@"NULL %i", row);
}
在运行时,如果我没有滚动,我会NSLog
在控制台窗口中看到一条语句:NULL 5
,当我向下滚动以显示最后一个单元格时,我会得到一条语句NSLog
:上下滚动,为什么会发生这种情况,尽管所有单元格都已经创建?NULL 0
NSLog
cellForRowAtIndexPath
代码是:
static NSString *customCellIdentifier = @"customCell";
EditableTableDataRow *cell = nil;
cell = (EditableTableDataRow *)[tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
if (cell == nil)
{
cell = [[[EditableTableDataRow alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:customCellIdentifier itemPadding:0 scaleToFill:NO] autorelease];
}
然后我在这个自定义单元格内的标签和文本字段上做各种事情,最后:
...
return cell;
非常感谢你。