【问题标题】:NSIndexPath return nilNSIndexPath 返回零
【发布时间】:2015-10-28 07:17:42
【问题描述】:

我有一个带有表格视图的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

【问题讨论】:

    标签: ios uitableview nsindexpath


    【解决方案1】:

    尝试向 nextviewcontroller 发送委托,例如

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

    当从那个 viewController 遍历到当前的 table viewcontroller 时,传递那个委托。

    这可能对你有帮助...

    【讨论】:

    • 感谢您的建议。我怎样才能穿越回来?你能给我样品吗?因为我的所有数据都应该保留。
    • 你能告诉我你用来遍历当前视图控制器的代码和你用来显示下一个视图控制器的代码
    • controller.delegate = self; 将此行添加到您的代码中
    【解决方案2】:

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

    当然没有 IndexPath,因为您从未点击过一个单元格。基本上你忽略了完整的 tableview 机制。

    创建 tableviewcell 时,用索引标记按钮。不是最好的方法,但可以。

    【讨论】:

    • 我在点击表格行内的按钮后说(myBtn)
    【解决方案3】:

    设置按钮的标签等于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];
    
    }
    

    【讨论】:

      【解决方案4】:

      我建议你可以在 'viewWillAppear' 下写 [tableView reload]; 或者使用block等其他方式来实现

      【讨论】:

      • 即使我做了但没用
      【解决方案5】:

      我不会使用方法indexPathForRowAtPoint: 来获取indexPath。您可以使用不同的方式对其进行归档。首先添加一个UIView 类别:

      头文件:

      #import <UIKit/UIKit.h>
      
      @interface UIView (TableCell)
      
      -(UITableViewCell*)getTableCell;
      
      @end
      

      和实施

      @implementation UIView (TableCell)
      
      -(UITableViewCell *)getTableCell
      {
         while (self.superview) {
            if ([self.superview isKindOfClass:[UITableViewCell class]]) {
               return (UITableViewCell*)self.superview;
            }else {
               return [self.superview getTableCell];
            }
         }
         return nil;
      }
      

      现在,在您的onButtonPress: 中,您可以这样做:

      UITableViewCell *cell = [sender getTableCell];
      NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]
      

      【讨论】:

      • 这个方法有什么不同
      • 您的方法基于视图布局,如果视图尚未完成布局,则可能无法正常工作。这个方法保证只要你有一个单元格就返回一个索引。
      • 我是iOS平台的新手,请您解释一下我应该如何实现这个。
      • 这样我现在得到了我的单元格 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
      • 这样我得到按钮 btn=(UIButton*)[cell viewWithTag:111];
      猜你喜欢
      • 2016-08-09
      • 1970-01-01
      • 2021-09-23
      • 2016-11-24
      • 2021-10-24
      • 2017-11-19
      • 2015-01-23
      • 2016-11-24
      • 2013-12-04
      相关资源
      最近更新 更多