这是Objective C中的保留周期吗?

时间:2014-12-05 22:55:01

标签: ios objective-c automatic-ref-counting objective-c-blocks

我已经在我的UICollectionViewCell上声明了一个属性:

@property (nonatomic, copy) void(^onSelection)(BOOL selected);

我这样覆盖-setSelected:

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];

    if (self.onSelection != NULL) {
        self.onSelection(selected);
    }
}

然后在-cellForItemAtIndexPath:我这样配置

cell.onSelection = ^(BOOL selected) {
    //the compiler is telling me this might be a retain cycle but i dont think so...
    cell.tintColor = [UIColor redColor];
};

这是保留周期吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

是的。相反,你应该使用弱+强组合。

__weak typeof(cell) weakCell = cell;
cell.onSelection = ^(BOOL selected) {
    __strong typeof(weakCell) strongCell = weakCell;
    //the compiler is telling me this might be a retain cycle but i dont think so...
    strongCell.tintColor = [UIColor redColor];
};

在您的特定情况下,您甚至不需要此块,因为您可以更新setSelected:内的子类中的单元格或处理表视图控制器中的tableView:didSelectRowAtIndexPath: