【发布时间】:2010-02-18 06:13:37
【问题描述】:
有人对如何清除缓存的UITableViewCell 有建议吗?
我想用重用标识符缓存这些单元格。但是,有时我需要删除或修改某些表行。我希望在行更改后致电reloadData。
现在,dequeueReusableCellWithIdentifier 总是返回之前缓存的(过时的)条目。如何指示缓存已过时且需要清除?
【问题讨论】:
标签: ios uitableview reuseidentifier
有人对如何清除缓存的UITableViewCell 有建议吗?
我想用重用标识符缓存这些单元格。但是,有时我需要删除或修改某些表行。我希望在行更改后致电reloadData。
现在,dequeueReusableCellWithIdentifier 总是返回之前缓存的(过时的)条目。如何指示缓存已过时且需要清除?
【问题讨论】:
标签: ios uitableview reuseidentifier
我不确定您为什么要首先清除单元格。每次使单元格出列时,都需要重新设置任何需要显示的数据。缓存只是防止您每次都必须设置任何不变的属性。但必须设置正在显示的实际数据,即使单元格之前已缓存。
请注意,对于同一类型的所有单元格,您的重用标识符应该相同。如果您正在做一些愚蠢的事情,例如根据相关行计算标识符,那么您做错了。
您的代码应该类似于
- (UITableViewCell *)tableView:(UITableView *)view cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 如果(!单元格){ // 没有缓存单元格,在这里新建一个 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; // 设置所有此类单元格应共享的任何属性,例如附件或文本颜色 } // 为这个特定的单元格设置数据 cell.textLabel.text = @"foo"; 返回单元格; }在该示例中,请注意我如何始终为单元格设置数据,并且所有单元格共享相同的标识符。如果您遵循此模式,则根本没有理由尝试“清除”您的单元格,因为任何旧数据都会被覆盖。如果您有多种类型的单元格,您可能希望在这种情况下使用多个标识符,但每个单元格类型仍然是 1 个标识符。
【讨论】:
dequeue,那么您再次“泄漏”您创建的每个单元格。您不是在保存 one alloc/init,而是在保存 EVERY SINGLE alloc/init 超过前 7 个(左右,取决于单元格高度和表格高度,基本上只是numCellsVisible + 2)。
UITableView 对可重用单元进行排队,因此一旦 UITableView 解除分配,队列就会被释放,因此所有缓存的单元也会被释放。
我不知道如何清除缓存,但是,当需要更改行时,我使用了一种解决方法来处理这种情况。
首先,我不使用静态单元格标识符。我要求数据对象生成它(注意[myObject signature],它提供了一个描述所有需要属性的唯一字符串):
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyObject *myObject = _dataSource[indexPath.row];
NSString *cellId = [myObject signature];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
[myObject configureCell:cell];
}
[myObject updateCellWithData:cell];
return cell;
}
[myObject signature] 为不同的属性列表提供不同的签名。所以,如果我的对象发生了变化,我只需调用[self.tableView reloadData],myObject 会提供一个新的签名,表格会为其加载一个新的单元格。
[myObject configureCell:cell] 将所有需要的子视图放置到单元格中。
[myObject updateCellWithData:cell] 用当前数据更新单元格的子视图。
【讨论】:
while ([tableView dequeueReusableCellWithIdentifier:@"reuseid"]) {}
【讨论】:
我找到了一个非常好的方法。
在.h中
//add this
int reloadCells;
在.m中
- (void)dumpCache {
reloadCells = 0;
[tableView reloadData];
}
-(void)tableView:(UITableView *)tableView cellForRowAtUndexPath:(NSIndexPath *)indexPath {
static NSString *CellID = @"Cell"
UITableViewCell *cell = [tableView dequeCellWithReuseIdentifier:CellID];
if (cell == nil || reloadCells < 12) {
cell = [[UITableViewCell alloc] initWithFormat:UITableViewCellStyleDefault reuseIdentifier:CellID];
reloadCells ++;
}
cell.textLabel.text = @"My Cell";
return cell;
}
【讨论】:
(void) dumpCache如何被调用?
我今天不得不做类似的事情。我有一个图像库,当用户将它们全部删除时,库需要摆脱最后一个排队的单元格并将其替换为“画廊空”图像。当删除例程完成时,它会将“清除”标志设置为 YES,然后执行 [tableView reloadData]。 cellForRowAtIndexPath 中的代码如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
if (purge) {
cell = nil;
purge = NO;
}
if (cell == nil) {
//*** Do usual cell stuff
}
return cell
}
【讨论】:
我正在编写 ObjC(手动引用计数)代码,发现一些奇怪的行为会阻止 UITableView 和 UITableViewCells 被释放,尽管我的内存管理是正确的。 UITableView 和 UITableViewCell 似乎相互保留。是的,我找到了一种强制 UITableView 及其单元格相互释放的方法(删除缓存的单元格)。
要做到这一点,基本上你需要一个表格视图来获得一个可重用的单元格。表格视图将从其可重用单元缓存中删除它。毕竟,您告诉该单元格从其超级视图中删除。完成。
现在作为一个独立的类...首先,您需要一个包含您使用的所有单元格标识符的数组。接下来,您实现一个虚拟数据源并让该数据源通过使用“ClearAllCachedCells”数据源重新加载自身来清除 UITableView:
#import <UIKit/UIKit.h>
@interface ClearAllCachedUITableViewCellsDataSource : NSObject <UITableViewDataSource, UITableViewDelegate>
+(void)clearTableView:(UITableView*)tv
reuseIdentifiers:(NSArray<NSString*>*)cellIdentifiers
delegateAfterFinish:(id<UITableViewDelegate>)dg
dataSourceAfterFinish:(id<UITableViewDataSource>)ds;
@end
魔法发生在 .m 文件中:
#import "ClearAllCachedUITableViewCellsDataSource.h"
@interface ClearAllCachedUITableViewCellsDataSource () {
BOOL clearing;
}
@property (nonatomic, readonly) UITableView *tv;
@property (nonatomic, readonly) NSArray<NSString*> *identifiers;
@property (nonatomic, readonly) id ds;
@property (nonatomic, readonly) id dg;
@end
@implementation ClearAllCachedUITableViewCellsDataSource
-(void)dealloc {
NSLog(@"ClearAllCachedUITableViewCellsDataSource (%i) finished", (int)_tv);
[_tv release];
[_ds release];
[_dg release];
[_identifiers release];
[super dealloc];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
[self performSelectorOnMainThread:@selector(clear) withObject:nil waitUntilDone:NO];
NSLog(@"TV (%i): reloading with zero cells", (int)_tv);
return 0;
}
-(void)clear {
if (!clearing) {
clearing = YES;
for (NSString *ident in self.identifiers) {
UITableViewCell *cell = [_tv dequeueReusableCellWithIdentifier:ident];
while (cell) {
NSLog(@"TV (%i): removing cached cell %@/%i", (int)_tv, ident, (int)cell);
[cell removeFromSuperview];
cell = [_tv dequeueReusableCellWithIdentifier:ident];
}
}
self.tv.delegate = self.tv.delegate == self ? self.dg : self.tv.delegate;
self.tv.dataSource = self.tv.dataSource == self ? self.ds : self.tv.dataSource;
[self release];
}
}
+(void)clearTableView:(UITableView*)tv
reuseIdentifiers:(NSArray<NSString*>*)cellIdentifiers
delegateAfterFinish:(id<UITableViewDelegate>)dg
dataSourceAfterFinish:(id<UITableViewDataSource>)ds {
if (tv && cellIdentifiers) {
NSLog(@"TV (%i): adding request to clear table view", (int)tv);
ClearAllCachedUITableViewCellsDataSource *cds = [ClearAllCachedUITableViewCellsDataSource new];
cds->_identifiers = [cellIdentifiers retain];
cds->_dg = [dg retain];
cds->_ds = [ds retain];
cds->_tv = [tv retain];
cds->clearing = NO;
tv.dataSource = cds;
tv.delegate = cds;
[tv performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
}
@end
【讨论】:
应使用消息prepareForReuse 清除先前使用单元格的旧数据。当单元格出列时,此消息会在从dequeueReusableCellWithIdentifier: 返回之前发送到该单元格。
【讨论】:
prepareForReuse: 方法会在每个单元格出列时发送到每个单元格,然后再交给您。这是自动发生的事情,只有在实现自己的 UITableViewCell 子类时才关心它。
没办法,我觉得…… 我使用了完全不同的方法。我没有依赖 UITableView 缓存,而是自己构建。通过这种方式,我可以完美地控制何时以及清除什么。 像这样的...
给定:
NSMutableDictionary *_reusableCells;
_reusableCells = [NSMutableDictionary dictionary];
我能做到:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"whatever-you-need";
UITableViewCell *cell = [_reusableCells objectForKey:cellIdentifier];
if (cell == nil)
{
// create a new cell WITHOUT reuse-identifier !!
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// store it in my own cache
[_reusableCells setObject:cell forKey:cellIdentifier];
/* ...configure the cell... */
}
return cell;
}
和:
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[_reusableCells removeAllObjects];
}
希望这会有所帮助。
【讨论】: