我有一个 tableview,它在自定义 UITableViewCell 中使用 UIButton,在我的 View Controller 中触发 IBAction。在 IOS7 中,通过执行此操作,我能够引用触发操作的特定单元格 UITableViewCell *clickedCell = (UITableViewCell *)[[[sender superview] superview]superview]
。
这在 iOS7 中运行良好,但在 iOS8 中似乎需要删除 superview
之一才能返回单元格,否则返回 nil。所以我现在就是这样做的,而且它有效,但我想知道是否有更简洁的方法来实现它。我只是不喜欢这个样子。
UITableViewCell *clickedCell;
if (SYSTEM_VERSION_LESS_THAN(@"8.0")) {
clickedCell = (UITableViewCell *)[[[sender superview] superview]superview];
}
else{
clickedCell = (UITableViewCell *)[[sender superview]superview];
}
最佳答案
您可以将 UIButton 子类化为 MYButton 并添加如下属性:
@property (nonatomic, weak) UITableViewCell *parentCell;
然后,当您从设置 parentCell = self 的单元格设置 UIButton 时
从那里您可以通过以下方式简单地访问单元格:
MYButton *button = (MYButton *)sender;
clickedCell = sender.parentCell.
确保使用弱属性,以免创建循环保留。
关于ios - 从 IBAction 获取对 UITableViewCell 的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26197611/