iphone - 如何dequeueReusableCellWithIdentifier : work?

标签 iphone ios uitableview

我想要关于 dequeueReusableCellWithIdentifier:kCellIdentifier 的一些精确度。如果我理解得很好,下面的 NSLOG 应该只打印一次。但事实并非如此。那么 dequeueReusableCell 有什么意义呢?它只对自定义单元有效吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCellIdentifier = @"UITableViewCellStyleSubtitle3";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (cell == nil)
    {
        NSLog(@"creation of the cell");
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier] autorelease];
    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [[self.table objectAtIndex:indexPath.row] objectForKey:kTitleKey];


    [cell setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.6]];
    return cell;
}

最佳答案

只有当初始化的单元格移出屏幕时,它才会发挥作用。

例如,假设您有一个表格 View ,它在屏幕上显示十个单元格,但总共有一百行。首次加载 View 并填充 TableView 时,将初始化十个单元格(因此有多个 NSLog statements)。当您开始向下滚动时,从屏幕顶部消失的单元格将被放入重用队列。当需要绘制从底部出现的新单元格时,它们将从重用队列中出队,而不是初始化新实例,从而降低内存使用量。

这也是为什么在 if (cell == nil) 条件之外配置单元格属性很重要的原因。

关于iphone - 如何dequeueReusableCellWithIdentifier : work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6745114/

相关文章:

iphone - h 264 硬件编码/解码 IOS(iPhone/Ipad)?

iphone - 如何通过App Store分发内部应用程序?

ios - 使用 Firebase Swift 检索数据错误

objective-c - 索引路径处行的单元格延迟

iphone - 如何获取UIView的图形上下文?

ios - 使用 iPhone 作为外围设备时的连接问题

ios - 删除申请时会删除文件文件夹吗?

iOS:AFNetworking 的可达性

ios - 如何将数据从一个表格 View 单元格传输到另一个表格 View 单元格?

swift - 出队后,CALayer 在自定义 UITableViewCell NIB 上消失