如何清除缓存的UITableViewCell [英] How to purge a cached UITableViewCell

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

问题描述

有没有人有关于如何清除缓存 UITableViewCell 的建议?

Does anyone have suggestions on how to purge a cached UITableViewCell?

我想缓存这些具有reuseIdentifier的单元格。但是,有时我需要删除或修改某些表行。我希望在行更改后调用 reloadData

I'd like to cache these cells with reuseIdentifier. However, there are times when I need to delete or modify some of the table rows. I expect to call reloadData after the row changes.

现在, dequeueReusableCellWithIdentifier 始终返回之前的缓存(过时)条目。如何指示缓存是否陈旧且需要清除?

Right now, dequeueReusableCellWithIdentifier always returns the cached(obsolete) entry from before. How do I indicate that the cache is stale and needs to be purged?

推荐答案

我不确定你为什么要尝试首先清除细胞。每次出列单元格时,都需要重新设置需要显示的数据。缓存只会阻止您每次都必须设置任何不变的属性。但是必须设置正在显示的实际数据,即使该单元格之前已被缓存。

I'm not sure why you're trying to purge cells in the first place. Every time you dequeue a cell, you need to re-set any data that needs to be displayed. The caching just prevents you from having to set up any non-changing properties every time. But the actual data that's being displayed must be set, even if the cell was cached before.

请注意,对于所有单元格,您的重用标识符应该相同。相同的类型。如果你正在做一些愚蠢的事情,比如根据有问题的行计算标识符,那么你做错了。

Note that your reuse identifier is supposed to be the same for all the cells of the same type. If you're doing something silly like calculating the identifier based on the row in question, then you're doing it wrong.

你的代码应该看起来像


- (UITableViewCell *)tableView:(UITableView *)view cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        // no cached cell, create a new one here
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
        // set any properties that all such cells should share, such as accessory, or text color
    }
    // set data for this particular cell
    cell.textLabel.text = @"foo";
    return cell;
}

在该示例中,请注意我每次都是如何设置单元格的数据,并且所有单元格共享相同的标识符如果您遵循这种模式,您根本没有理由尝试清除您的单元格,因为任何旧数据都会被覆盖。如果您有多种类型的单元格,在这种情况下您可能希望使用多个标识符,但每个单元格类型仍然是1个标识符。

In that example, note how I always set the data for the cell every single time, and all cells share the same identifier. If you follow this pattern, you should have no reason at all to try and "purge" your cells, as any old data will be overwritten anyway. If you have multiple types of cells, you may want to use multiple identifiers in that case, but it's still 1 identifier per cell type.

这篇关于如何清除缓存的UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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