我有一个带有表视图的UIViewController。表行包含触发操作的按钮。
//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
发布于 2015-11-12 09:31:01
尝试向delegate控制器发送委托,如下所示
DestinationViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationViewController"];
controller.args=myArgs;
controller.delegate = self;
[self.navigationController pushViewController:controller animated:YES];当从该viewController返回到当前表视图控制器时,传递该委托。
这也许能帮你..。
发布于 2015-10-28 07:31:17
如果我理解你是正确的,你在TableViewCells中有按钮,当你点击其中一个按钮时,你不会得到IndexPath。
当然没有IndexPath,因为你从来没有打过手机。基本上,您忽略了完整的tableview机制。
创建表视图单元格时,用索引标记按钮。这不是最好的方法,但会成功的。
发布于 2015-10-28 08:50:06
将按钮的标记设置为indexPath.row,并从标记中找到NSIndexPath。
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];
}https://stackoverflow.com/questions/33385070
复制相似问题