ios - 这是 Objective C 中的保留循环吗?

标签 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];
};

这是一个保留周期吗?

谢谢!

最佳答案

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

__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];
};

在您的特定情况下,您甚至不需要此 block ,因为您可以更新 setSelected: 内子类中的单元格或在表格 View 中处理 tableView:didSelectRowAtIndexPath: Controller 。

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

相关文章:

iOS 8+ 从后台恢复应用程序时核心数据故障

ios - 我可以将适用于 iOS 的 GoogleMaps SDK 链接到除 -ObjC 之外的链接器标志吗

objective-c - 如何将应用程序迁移到 iOS 5? (或打开 ARC)

xcode - 为什么内联汇编不能与 Xcode 中的 ARC 一起使用?

ios - 递归函数,带有完成 block ,检索多个 MKDirections - Swift

ios - x 和 y 位置因不同的设备而异

ios - 我想用 1 到 100 的值填充 array2,但保留 array1 中存在的值

iphone - 在 UITableView 中,取消屏幕外单元格的 GCD 操作的最佳方法是什么?

iphone - 正确排序 NSString 数组,包括以破折号或特殊符号开头的单词(对于字典)

svg - 两个完全相同的 SVG 路径绘制方式不完全相同