如果我打电话:
-[UITableView dequeueReusableCellWithIdentifier]然后选择而不是来重用此方法中的单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath一定要漏水吗?我的猜测是,一旦自动释放池排水,它将是dealloced。
发布于 2013-03-22 21:00:25
是的,dequeueReusableCellWithIdentifier返回一个自动释放的对象,不会导致内存泄漏。
但是,如果您选择不重用单元格(无论出于什么原因),那么就没有必要首先调用dequeueReusableCellWithIdentifier。
更新:您可以使用以下代码检查内部重用队列中存储了多少单元格:
NSDictionary *reuseDict = [self.tableView valueForKey:@"reusableTableCells"];
NSArray *reuseArray = [reuseDict objectForKey:CellIdentifier];
NSLog(@"%d", [reuseArray count]);我用Master应用程序进行了测试,在这个应用程序中,我删除了对dequeueReusableCellWithIdentifier的调用。重复使用队列中的单元数在景观方向上最多为17个,在纵向中为23个。这正是可见细胞加一个细胞的数量。因此,数量确实是有限的,即使您从未重用单元。
发布于 2013-03-24 21:40:46
是的,这会导致泄密。通过子类UITableViewCell来体验这一点
static int instances = 0;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSLog(@"Init! instances=%d", instances++);
}
return self;
}
-(void)dealloc {
NSLog(@"Dealloc! instances=%d", --instances);
}如果您不使用从dequeueReusableCellWithIdentifier返回的单元格,就会有泄漏。
这个问题有两种解决办法:
dequeueReusableCellWithIdentifier的结果nil作为reuseIdentifier;这样就不会引入泄漏。这是最好的解决方案,除非您的表有很多行,而且您不想处理重用的混乱。发布于 2015-02-05 11:52:42
如果您不返回由UITableViewCell在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;中返回的dequeueReusableCellWithIdentifier,那么它可能会产生泄漏或不泄漏,这取决于您正在做什么。dequeueReusableCellWithIdentifier返回的单元格在释放表视图时会自动释放,因为表视图包含对此方法返回的每个单元格的引用。
例如,如果您在dequeueReusableCellWithIdentifier中重复调用- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath,则会出现问题:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
// calculate height
// ...
}在上面的代码中,每次调用该方法时,dequeueReusableCellWithIdentifier都会返回新的单元格,因此当您滚动表视图时,内存使用量将上升。但是,在以后的某个时间点,dequeueReusableCellWithIdentifier分配的这些单元将在表视图重新生成时自动释放。
https://stackoverflow.com/questions/15579228
复制相似问题