IOS 7 – 如何从放置在UITableViewCell中的button获取indexPath

我已经编程创build了一个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; 

这是button被点击后调用的方法。

 - (void)changeSwitch:(UISwitch *)sender{ UITableViewCell *cell = (UITableViewCell *)[sender superview]; NSIndexPath *indexPath = [self.filterTableView indexPathForCell:cell]; NSLog(@"%ld",(long)indexPath); ……………. My other code……. } 

我能够在iOS 6中打印索引path的值,但在iOS 7中打印零,

我是否缺lessiOS 7中的任何内容,或者有其他的方法来获取iOS 7中的indexPath

谢谢阿伦

NSLog(@"%ld",(long)indexPath); 这是错误的,将打印indexPath的指针地址

尝试使用以下代码

 CGPoint center= sender.center; CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.filterTableView]; NSIndexPath *indexPath = [self.filterTableView indexPathForRowAtPoint:rootViewPoint]; NSLog(@"%@",indexPath); 

如果你有单元格中的button。 你可以通过调用superview来获取单元格。 然后可以通过这种方式得到Indexpath。

  (void)obButtonTap:(UIButton *)sender { UITableViewCell *cell = (UITableViewCell *)sender.superview; NSIndexPath *indexPath = [tableView indexPathForCell:cell]; } 

Swift解决scheme:像这样的UITableView扩展可以用于这个。

 extension UITableView { func indexPathForView(view: AnyObject) -> NSIndexPath? { let originInTableView = self.convertPoint(CGPointZero, fromView: (view as! UIView)) return self.indexPathForRowAtPoint(originInTableView) } } 

用处变得容易到处。

 let indexPath = tableView.indexPathForView(button) 

你可以给cellForRowAtIndexPath方法中的每个开关分配标签

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero]; [accessorySwitch setOn:NO animated:YES]; [accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged]; cell.accessoryView = accessorySwitch; accessorySwitch.tag = indexPath.row; } - (void)changeSwitch:(UISwitch *)sender{ NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:[sender tag]]; NSLog(@"%ld",(long)indexPath); } 

你可能会被烧毁,假设交换机的superview将成为tableViewCell 。 也许在iOS 7中,他们改变了层次结构以达到某种视觉效果; 也许在那里插入一个新的观点。

你可能可以indexPath UISwitch并给它一个indexPath属性。 然后在你的cellForRowAtIndexPath ,在那里分配它,所以你有一个参考。

我认为依靠button的位置知道哪个被点击是危险的。

我prefere创build一个字典作为一个关键的button/开关本身,并作为一个值的索引path:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ ... UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; NSValue * accessorySwitchKey = [NSValue valueWithNonretainedObject:[cell settingSwitch]]; [[self switchIndexDictionary]setObject:indexPath accessorySwitchKey]; ... } 

那么当开关/button被触发时,我可以很容易地从我的字典中获得indexpath:

 - (void)toggleSetting:(id)sender { UISwitch *selectedSwitch = (UISwitch *) sender; NSValue * accessorySwitchKey = [NSValue valueWithNonretainedObject:selectedSwitch]; NSIndexPath *indexPath = [[self switchIndexDictionary]objectForKey: accessorySwitchKey]; } 

SWIFT 4:

 extension UITableView { func indexPathForView(view: AnyObject) -> NSIndexPath? { let originInTableView = self.convert(CGPoint.zero, from: (view as! UIView)) return self.indexPathForRow(at: originInTableView)! as NSIndexPath } }