ios - dequeueReusableCellWithIdentifier 然后不使用单元格?

标签 ios uitableview memory-leaks uikit

如果我打电话:

-[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/

    相关文章:

    iOS + UIlabel 在 UITableViewCell 中带有开始和结束引号

    ios - 向上滚动 UITableView 单元格然后展开它

    ios - Swift 3 UITableViewCell indexPath.row 搞砸了

    uiview - 可插入自定义 View Nib (Nib-in-a-Nib) : Memory leak – why?

    c++ - 在 C++ 中,返回从本地 char 数组创建的字符串会导致内存泄漏或未定义的行为吗?

    c - 如何使用Valgrind构建动态库?

    ios - 数组支持任何类型的值

    iphone - 使用阻塞或类似方法从 NSURLConnection 获取数据

    javascript - 如何使用 Jquery :focus selector on input elements in iOS?

    ios - 如何根据表格行设置动态表格高度?