加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 17:18:34 所属栏目:百科 来源:网络整理
导读:我有动态tableView和一个问题.在每个tableCell中有两个按钮. 我试图让按钮触及indexPath.row但没有成功. 我的代码: - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ NSIndexPath *indexPath = [self.homeTableView indexPathForSel
我有动态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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读