我已经在 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/