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

参考文章: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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容