ios – 如何通过触摸按钮获取indexPath?

前端之家收集整理的这篇文章主要介绍了ios – 如何通过触摸按钮获取indexPath?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有动态tableView和一个问题.在每个tableCell中有两个按钮.
我试图让按钮触及indexPath.row但没有成功.

我的代码

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    NSIndexPath *indexPath = [self.hoMetableView indexPathForSelectedRow];      

    NSLog(@"%d",indexPath.row);


}

我怎样才能获得indexPath.row for touced按钮(Segue连接通过这两个按钮 – >两个连接)?

解决方法

它不起作用,因为单击按钮时没有选择行.

使用两个按钮,我会断开你的segue与按钮,并在IB中做2个手动segue然后添加这样的代码来处理它们:

-(void)button1Pressed:(id)sender {
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonIndexPath = [self.hoMetableView indexPathForCell:clickedCell];
    ...
    [self performSegueWithIdentifier:@"YourManualSegueIdentifier1" sender:self];
}

编辑第二个选项:

在MyTableViewCell.h中:

@interface MyTableViewCell : UITableViewCell
    ...    
    @property (strong,nonatomic) NSIndexPath *cellIndexPath;
    ...
@end

在MyTableViewCell.m中:

-(void)button1Pressed:(id)sender {
    ...
    NSInteger row = cellIndexPath.row;
    ...
}

在MyTableViewController.m中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];      
    }
    // remember index here!
    [cell setCellIndexPath:indexPath];
    ....
    return cell;
}

猜你在找的Xcode相关文章