OGeek|极客世界-中国程序员成长平台

标题: iphone - 最初多次调用 tableView cellForRowAtIndexPath [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:39
标题: iphone - 最初多次调用 tableView cellForRowAtIndexPath

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

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 之后再次调用它? (挠头……)



Best Answer-推荐答案


滚动表格 View 时,屏幕上可能有 9 个单元格。这在您开始时不需要,但是当您滚动时,需要第 9 个单元格作为顶部的离开,底部的新单元格进入。一个单元格不能分成两半重复使用。

关于iphone - 最初多次调用 tableView cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6567596/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4