我以编程方式创建了一个UITableView,并将UISwitch添加到它的单元附件视图中.
这是我在cellForRowAtIndexPath方法中的单元附件视图中的UISwitch代码.
UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero];
[accessorySwitch setOn:NO animated:YES];
[accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = accessorySwitch;
Run Code Online (Sandbox Code Playgroud)
这是单击按钮后调用的方法.
- (void)changeSwitch:(UISwitch *)sender{
UITableViewCell *cell = (UITableViewCell *)[sender superview];
NSIndexPath *indexPath = [self.filterTableView indexPathForCell:cell];
NSLog(@"%ld",(long)indexPath);
……………. My other code…….
}
Run Code Online (Sandbox Code Playgroud)
我能够在iOS 6中打印索引路径值但是在iOS 7中它打印为nil,
我在iOS 7中遗漏了什么,或者在iOS 7中有其他方法来获取indexPath
谢谢,阿伦.