简体   繁体   English

这是目标C中的保留周期吗?

[英]is this a retain cycle in Objective C?

I've declared a property on my UICollectionViewCell like this: 我在UICollectionViewCell上声明了一个属性,如下所示:

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

I override -setSelected: like this: 我重写-setSelected:像这样:

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

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

Then in -cellForItemAtIndexPath: i configure like this 然后在-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];
};

Is this a retain cycle? 这是保留周期吗?

Thanks! 谢谢!

Yes it is. 是的。 Instead you should use a weak+strong combo. 相反,您应该使用弱+强组合。

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

In your particular case you don't even need this block because you can update cell in subclass inside of setSelected: or handle tableView:didSelectRowAtIndexPath: in your table view controller. 在您的特定情况下,您甚至不需要此块,因为您可以更新setSelected:内的子类中的单元格,或在表视图控制器中处理tableView:didSelectRowAtIndexPath:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM