您的当前位置:首页正文

自定义tableView的cell上添加button按钮获取ce

来源:要发发知识网

当我们在一个自定义的tableView Cell上添加一个UIButton按钮时,要想点击按钮时获取对应的indexPath,这时发现没有什么好的办法直接拿到而不能实现点击按钮跳转到cell对应的页面。这时不妨试试下面的方法

cell上面添加button就不必多说了,直接从button的点击事件开始

- (void)btnAction:(UIButton *)sender{

  //1. 将button强转成你自定义cell类(GoodsListCell是我的自定义cell)
    GoodsListCell *cell = (GoodsListCell *)[sender superview];
  //2.使用tableView indexPathForCell来获取对应的indexPath
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    BuyViewController *buyVC = [[BuyViewController alloc]init];
  //3.获取cell对应的indexPath就可以实现传值了
    buyVC.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:buyVC animated:YES];
}

一个小技巧,在一些类似购物这种App中会用到,希望会帮助到你!!!