我在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];
};

这是保留周期吗?

谢谢!

最佳答案

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

__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:

关于ios - 这是 objective-c 中的保留周期吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27326004/

10-13 03:52