gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:02:48 26 4
gpt4 key购买 nike

我已经像这样在我的 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: 子类中的单元格或处理 tableView:didSelectRowAtIndexPath: 在您的 TableView 中 Controller 。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com