美文网首页
cell使用block导致循环引用问题

cell使用block导致循环引用问题

作者: ioido | 来源:发表于2017-04-27 14:12 被阅读0次

参考文章:http://www.jianshu.com/p/b33e5989a352
mvc分离自定义的cell里使用strong修饰,会导致controller无法dealloc,反复进页面占用内存不断增大;使用assign修饰,点击按钮会崩溃。
正确的用法是使用strong,在controller中使用weakSelf;
eg

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"HistoryTableViewCell";
    HistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:identifier owner:nil options:nil] firstObject];
    }
    cell.model = self.datalist[indexPath.row];
    __weak typeof(self) weakSelf = self;
    cell.block = ^(UIButton *btn) {
        [weakSelf dealWithBtn:btn];
    };
    return cell;
}

相关文章

网友评论

      本文标题:cell使用block导致循环引用问题

      本文链接:https://www.haomeiwen.com/subject/qfogzttx.html