ios - 如何确定带有已点击开关的单元格的 indexPath?

标签 ios uitableview uiswitch

我有一个带有原型(prototype)单元格的 UITableView,上面有一个 UISwitch。当用户点击任何特定单元格上的开关时,我需要确定我的数据源中的哪个对象已切换开关。

这是我的切换方法:

- (IBAction)completeSwitchTapped:(id)sender {
      UITableViewCell *cell = (UITableViewCell *)[sender superview];
      NSIndexPath *indexPath = [self.itineraryTableView indexPathForCell:cell];


      NSLog(@"cell row is: %d in section: %d", indexPath.row, indexPath.section);
}

但是无论选择了哪一行或哪一节,这总是返回第 0 节中的第 0 行。

如何正确返回包含开关的正确单元格?显然,[sender superview] 没有工作,我对如何引用单元格一筹莫展。

谢谢!

最佳答案

尝试这样的事情:

UIView *superview = sender.superview;
while (![superview isKindOfClass:[UITableViewCell class]] && superview.superview != nil) {
    superview = superview.superview;
}
NSIndexPath *indexPath = [self.itineraryTableView indexPathForCell:superview];

关于ios - 如何确定带有已点击开关的单元格的 indexPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23575513/

相关文章:

ios - 如何将两个 UIImage 添加到 UITableView 背景

ios - 如何在打开和关闭 UIswitch 时上下移动标签?

iphone - 更改 UITableview 中多个开关的值

ios - 在闭包中使用可变参数时编译器崩溃

ios - 下载缩略图后重新加载 UItableViewCell

ios - 事件指示器不在中心

iphone - 自定义绘制的 UITableViewCell 中的可访问性

ios - 默认 UISwitch 为 YES 然后使用 NSUserDefaults 保存状态

ios - 有没有一个好方法来检查变量在连续更改一段时间后何时停止更改其值?

ios - 在“Cirrious.MvvmCross.Plugins.DownloadCache.MvxFileDownloadCache/Timer”中找不到嵌套类型