iphone - tableView cellForRowAtIndexPath 最初被多次调用

标签 iphone objective-c ios

我试图了解每个单元格的渲染如何工作。这是我的代码:

- (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/

相关文章:

javascript - 如何让 jQuery UI 的 Draggable 和 Sortable 功能在 iPhone 上运行?

objective-c - Obj-C 到 Swift : Block to Closure/Delegate

iphone - 如何摆脱控制台中每个 NSLog 语句之前出现的日期和时间

iphone - 使用多个 Facebook 应用 ID 的 iOS 应用

iphone - 不带后退按钮的当前 ViewController

ios - 如何在 Sprite Kit 中创建一个永远循环的自动反转动画?

iphone - 用户界面文本字段 : testing text property for empty string or nil or null doesn't work

ios - 一些具有动态高度的表格 View 单元格,其他具有固定高度

ios - iOS Objective-C 中的 URL 构建有哪些 native 类?

iphone - 将地址强制转换为 (id) 有副作用吗???地址 0xbfffe8d0 是否特殊? (固定 : issue was with _NSCallStackArray)