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

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

问题描述

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

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

UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero];[accessorySwitch setOn:NO 动画:YES];[accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];cell.accessoryView = 附件开关;

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

- (void)changeSwitch:(UISwitch *)sender{UITableViewCell *cell = (UITableViewCell *)[sender superview];NSIndexPath *indexPath = [self.filterTableView indexPathForCell:cell];NSLog(@"%ld",(long)indexPath);………………我的其他代码……}

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

我是否在 iOS 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);

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

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…….
}

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

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

Thanks, Arun.

解决方案

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

try using following codes

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天全站免登陆