IOS 7 - 如何从放置在UITableViewCell中的按钮获取indexPath [英] IOS 7 - How to get the indexPath from button placed in UITableViewCell

查看:132
本文介绍了IOS 7 - 如何从放置在UITableViewCell中的按钮获取indexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式创建了一个UITableView,并将UISwitch添加到它的单元附件视图中。

I've programmatically created a UITableView and added UISwitch to it's cell accessory view.

这是我在 cellForRowAtIndexPath 方法的单元附件视图中的UISwitch代码。

This is my code for UISwitch in cell accessory view in cellForRowAtIndexPath method.

UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero];
[accessorySwitch setOn:NO animated:YES];
[accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = accessorySwitch;

这是点击按钮后调用的方法。

This is the method which is called after the button is clicked.

- (void)changeSwitch:(UISwitch *)sender{

    UITableViewCell *cell = (UITableViewCell *)[sender superview];

    NSIndexPath *indexPath = [self.filterTableView indexPathForCell:cell];
    NSLog(@"%ld",(long)indexPath);

 ……………. My other code…….
}

我可以在iOS 6中打印索引路径值
但是在iOS 7中它打印为nil,

I am able to print the index path value in iOS 6 But in iOS 7 it prints nil,

我在iOS 7中遗漏了什么,或者在iOS 7中有其他方法来获取indexPath

Am i missing anything in iOS 7 or there is some other approach to get the indexPath in iOS 7

谢谢,
Arun。

Thanks, Arun.

推荐答案

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

NSLog(@"%ld",(long)indexPath); is wrong this will print the pointer address of indexPath

尝试使用以下代码

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

这篇关于IOS 7 - 如何从放置在UITableViewCell中的按钮获取indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆