我创建了两个或更多自定义单元格
每个电池内部都有一个开关
我如何知道我点击了行中的哪个开关
例如,我点击第3行的开关,它将返回indexPath.row = 3
开关状态是开还是关?
我应该放入哪个空白?
我知道有一种方法可以通过以下方式获取索引路径返回:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
但我不想碰行,只想碰开关......
哦,我做了一些研究,但只是有点不同
How can keep track of the index path of a button in a tableview cell?
有什么想法吗?
我希望我可以发布我的屏幕截图,但不是我是新用户 系统不允许我这样做
最佳答案
创建开关时,为其设置目标和操作:
[mySwitch addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
这里,目标 self
是您的 UITableViewController
,您需要在其中实现方法 (void) switchToggled:(id)sender
处理控制事件。切换时,开关将自动将自身发送到 switchToggled
方法作为 sender
。像这样(从您链接的 UIButton
示例修改):
- (void)switchToggled:(id)sender {
UISwitch *theSwitch = (UISwitch *)sender;
UITableViewCell *cell = (UITableViewCell *)theSwitch.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
if(theSwitch.on) {
// switch turned on
}
else {
// switch turned off
}
}
关于iphone - 当我点击连续的开关时,如何获取indexPath.row/path?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3620148/