indexPathForSelectedRow 返回错误的索引

indexPathForSelectedRow returning wrong index

当我点击放置在单元格中的按钮时,我想获取单元格中显示的一些数据。为此,我已经实现了使用 indexPathForSelectedRow 方法。但是当我点击单元格时,该方法返回 0 索引,即使我没有点击第 0 个索引单元格。这是我用于按钮的代码 -

- (IBAction)dataSaveLounge:(id)sender {

NSIndexPath *path = [self.loungeTable indexPathForSelectedRow];

NSString *titleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"title"];

NSString *subtitleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"excerpt"];

NSString *idSave = [[loungeData objectAtIndex:path.row] valueForKey:@"id"];


saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];

}

编辑:添加了 cellfForRowAtIndex 方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *loungeCellID = @"MyCellLounge";
LoungeTableCell *loungeCell = [tableView dequeueReusableCellWithIdentifier:loungeCellID];

if (loungeCell == nil) {

    loungeCell = [[LoungeTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCellLounge"];
}

loungeCell.loungeTitle.text = [[loungeData objectAtIndex:indexPath.row]valueForKey:@"title"];
loungeCell.loungeSubtitle.text = [[loungeData objectAtIndex:indexPath.row]valueForKey:@"excerpt"];

dispatch_async(dispatch_get_main_queue(), ^{

    loungeCell.loungeImgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"default"]];

    [loungeCell.loungeImgView sd_setImageWithURL:[NSURL URLWithString:[[loungeData objectAtIndex:indexPath.row] valueForKey:@"banner_image"]]];
});

return  loungeCell;
}

我在单击按钮时将一些数据保存在核心数据中,因此调用了最后一个方法

如果你点击行中的按钮试试这个 SET按钮标签

cell.button.tag = indexPath.row 
[cell.button addTarget:self action:@selector(dataSaveLounge:) forControlEvents:UIControlEventTouchUpInside];

在你的cellForRowAtIndexPath

- (IBAction)dataSaveLounge:(id)sender {
UIButton *pressedButton = (UIButton *)sender;
   NSLog(@"Tag of button pressed:%d",pressedButton.tag);
}

cellForRowAtIndexPath

中设置按钮标签

例如loungeCell.btn = indexPath.row;// set your button IBOutlet name here

然后在这里访问

- (IBAction)dataSaveLounge:(id)sender {

    UIButton *btn = (UIButton *)sender;


    NSString *titleSave = [[loungeData objectAtIndex:btn.tag] valueForKey:@"title"];

    NSString *subtitleSave = [[loungeData objectAtIndex:btn.tag] valueForKey:@"excerpt"];

    NSString *idSave = [[loungeData objectAtIndex:btn.tag] valueForKey:@"id"];


    saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];

    }

试试下面的代码:

将您的 dataSaveLounge 方法替换为以下方法:

- (IBAction)dataSaveLounge:(id)sender {

  CGPoint center= sender.center; 
  CGPoint viewPoint = [sender.superview convertPoint:center toView:self.loungeTable];
  NSIndexPath *path = [self.loungeTable indexPathForRowAtPoint:viewPoint];


  NSString *titleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"title"];

  NSString *subtitleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"excerpt"];

  NSString *idSave = [[loungeData objectAtIndex:path.row] valueForKey:@"id"];


  saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];

}

- (IBAction)dataSaveLounge:(id)sender {

      CGPoint btnPosition = [sender convertPoint:CGPointZero
                                   toView:self.tableView];
      NSIndexPath *path = [self.loungeTable indexPathForRowAtPoint:btnPosition];



      NSString *titleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"title"];

      NSString *subtitleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"excerpt"];

      NSString *idSave = [[loungeData objectAtIndex:path.row] valueForKey:@"id"];


      saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];

    }

在 UITableView 中为此目的使用标签是有风险的。如果您添加或删除任何行,现有可见行上的标记不会更改,您将指向错误的索引。

查看此答案以获得更好的解决方案

你也可以这样得到NSIndexPath。如果您有多个部分,那么此代码也可以帮助您。并确保 LoungeTableCell

中的按钮层次结构
- (IBAction)dataSaveLounge:(id)sender {
     LoungeTableCell *cell = (LoungeTableCell *) sender.superview.superview;
     NSIndexPath *path =  [self.loungeTable indexPathForCell:cell];
}