dequeueReusableCellWithIdentifier:如何工作? [英] How does dequeueReusableCellWithIdentifier: work?

查看:192
本文介绍了dequeueReusableCellWithIdentifier:如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对dequeueReusableCellWithIdentifier:kCellIdentifier做一些精确的介绍.如果我了解得很好,那么下面的NSLOG应该只打印一次.但事实并非如此.那么dequeueReusableCell的意义是什么?它仅对自定义单元格有效吗?

I would like some precision about the dequeueReusableCellWithIdentifier:kCellIdentifier. If I understand well, the hereunder NSLOG is supposed to print just once. But it doesn't. So what is the point of dequeueReusableCell ? Is it only efficient with custom cell ?

- (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;
}

推荐答案

开始在表格视图上滚动,您应该看到不再显示日志消息.

Start to scroll on your tableview and you should see that the log message doesn't appear anymore.

如果您有一个高度为1000像素的表格视图,并且每个单元格的高度为100像素,您将看到11次日志消息.
因为11是同时可见的最大单元数.
它是11,而不是10,因为当您向下滚动一点时,将有9个完全可见的单元格和2个仅部分可见的单元格.

if you have a tableview that has a height of 1000 pixels, and each cell has a height of 100 pixels you will see the log message 11 times.
Because 11 is the maximum amount of cells that are visible at the same time.
It's 11 and not 10 because when you scroll down a little there will be 9 cells that are fully visible and 2 cells that are only partial visible.

这篇关于dequeueReusableCellWithIdentifier:如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆