简体   繁体   English

UITableView不重用单元格吗?

[英]UITableView doesn't reuse cells?

Good morning all. 大家早上好。

I'm having a small crisis with table views in a 3.1 app. 我在3.1应用程序中遇到了表格视图的小危机。 I have a table view with some complex tableViewCells being loaded from a nib, and they're not being reused. 我有一个表格视图,其中有一些复杂的tableViewCells从笔尖加载,并且没有被重用。 Feeling it might have something to do with loading them from a nib, I decided to try a simple test project to see if cells are being reused using nothing but the basic apple template code. 感觉到它可能与从笔尖中加载有关,我决定尝试一个简单的测试项目,看看是否使用基本的Apple模板代码重用了单元格。 I started with the Navigation Based Template and here's the code for cellForRowAtIndexPath: 我从基于导航的模板开始,这是cellForRowAtIndexPath的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
    return cell;
}

I have a breakpoint set at the following line, and it's getting called on every pass. 我在下一行设置了一个断点,每次通过都会调用它。

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]

Setting the reuseIdentifier in the constructor as such: 像这样在构造函数中设置reuseIdentifier:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
    }

Yields the same result, no cell reuse. 产生相同的结果,没有单元重用。

Thanks in advance for any help you can offer! 在此先感谢您提供的任何帮助!

The table will usually create and use a few cells (5 or so usually) before it needs to start reusing them. 在开始重新使用它们之前,该表通常会创建并使用一些单元格(通常为5个左右)。 This of course depends on how big they are and how many appear on screen at any given time. 当然,这取决于它们的大小以及在任何给定时间出现在屏幕上的数量。 Nothing to fret about. 没什么可担心的。

I am not sure what you mean by everytime, everytime the app opens ? 我不确定每次打开应用程序时的意思是什么?

Run the code with object allocation tool and look at the graph for object allocations as you scroll through the tableview . 使用对象分配工具运行代码,并在表视图中滚动时查看图形中的对象分配。 If object allocations are increasing linearly even after few scrolls . 如果对象分配即使滚动几下也呈线性增长。 Then you have a problem. 那你有问题。

尝试使CellIdentifier静态化,例如

static NSString *CellIdentifier = @"Cell";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM