如果我打电话:
-[UITableView dequeueReusableCellWithIdentifier]
然后我选择不是 在此方法中重用单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
是否一定要引入泄漏?我的猜测是
dealloced
一旦自动释放池耗尽。
最佳答案
是的,这会导致泄漏。通过子类 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
;则不会引入泄漏。这是最好的解决方案,除非您的表有很多行,并且您不想处理重用的困惑。 关于ios - dequeueReusableCellWithIdentifier 然后不使用单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15579228/