首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSIndexPath返回零

NSIndexPath返回零
EN

Stack Overflow用户
提问于 2015-10-28 07:17:42
回答 5查看 1.3K关注 0票数 2

我有一个带有表视图的UIViewController。表行包含触发操作的按钮。

代码语言:javascript
复制
//This is my button selector inside tableview delegate cellForRowAtIndexPath

[myBtn addTarget:self action:@selector(onButtonPress:event:)forControlEvents:UIControlEventTouchUpInside];

//This is button action



 -(void)onButtonPress:(UIButton*)sender {
       CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
       NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];

       DestinationViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationViewController"];
       controller.args=myArgs;
       [self.navigationController pushViewController:controller animated:YES];


 }

//this is my back button action

- (IBAction)onBackPressed:(id)sender {
     [self.navigationController popViewControllerAnimated:YES];
}

第一次,一切正常,但在我导航到其他ViewController并返回到tableview控制器后,按后退按钮,再单击表行按钮,NSIndexPath为零。

我试过这个indexPathForRowAtPoint returns nil only for first cell in a uitableview

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2015-11-12 09:31:01

尝试向delegate控制器发送委托,如下所示

代码语言:javascript
复制
   DestinationViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationViewController"];
   controller.args=myArgs;
   controller.delegate = self;
   [self.navigationController pushViewController:controller animated:YES];

当从该viewController返回到当前表视图控制器时,传递该委托。

这也许能帮你..。

票数 0
EN

Stack Overflow用户

发布于 2015-10-28 07:31:17

如果我理解你是正确的,你在TableViewCells中有按钮,当你点击其中一个按钮时,你不会得到IndexPath。

当然没有IndexPath,因为你从来没有打过手机。基本上,您忽略了完整的tableview机制。

创建表视图单元格时,用索引标记按钮。这不是最好的方法,但会成功的。

票数 0
EN

Stack Overflow用户

发布于 2015-10-28 08:50:06

将按钮的标记设置为indexPath.row,并从标记中找到NSIndexPath。

代码语言:javascript
复制
myBtn.tag=indexPath.row;
[myBtn addTarget:self action:@selector(onButtonPress:)forControlEvents:UIControlEventTouchUpInside];

-(void)onButtonPress:(UIButton*)sender {

UIButton *btnSender =(UIButton *)sender;

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btnSender.tag inSection:0];

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33385070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档