我试图了解每个单元格的渲染如何工作。这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"QuestionCell";
static NSString *cellNib = @"QuestionsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellNib owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog(@"%@", @"--> load from nib");
}
return cell;
}
此代码中的 NSLog 最初被调用 8 次(因为我最初有 8 行可见)。然后当我开始滚动时,它会被调用一次,然后就不会再调用了。我认为它应该被调用一次,就是这样,因为它应该在第一次调用后重新使用它?为什么在最初的 8 之后再次调用它? (挠头……)
最佳答案
当您滚动表格 View 时,屏幕上可能有 9 个单元格。当您开始时不需要,但当您滚动时,需要第 9 个单元格,因为顶部的单元格离开,底部的新单元格进入。电池不能分成两半重复使用。 :)
关于iphone - tableView cellForRowAtIndexPath 最初被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6567596/