美文网首页
IOS 开发 如何获取点击的是哪个tableViewCell上的

IOS 开发 如何获取点击的是哪个tableViewCell上的

作者: 本客 | 来源:发表于2019-08-03 13:35 被阅读0次

在做开发中遇见的问题可谓是一波未平,一波又起呀,今天遇见了一个这样的问题,我做的是类似于淘宝的电商类型的APP,一个表格cell上有两个商品,我点击其中一个商品之后,怎么才能知道我点击的是哪个商品,跳转页面之后请求哪个商品的数据呢,我的逻辑就是,先需要获取到该商品所在哪一个cell中,通过对cell的下标来进行识别按钮的唯一标识,写出了代码,记录一下。

1.首先把cell上button按钮的点击方法写入在cell展示里面

//cell展示

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

    PraiseListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PraiseListCell" forIndexPath:indexPath];

//重点

 [cell.btn addTarget:self action:@selector(onTouchBtnInCell:) forControlEvents:(UIControlEventTouchUpInside)];

    return cell;

}

2.在button按钮的点击方法里面实现

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

    CGPoint point = sender.center;

    point = [self.tableView convertPoint:point fromView:sender.superview];

    NSIndexPath* indexpath = [self.tableView indexPathForRowAtPoint:point];

    NSLog(@"%ld",(long)indexpath.row);

}

3.能拿到点击的indexpath,我们就可以操作数据、数组、做自己要的东西啦

相关文章

网友评论

      本文标题:IOS 开发 如何获取点击的是哪个tableViewCell上的

      本文链接:https://www.haomeiwen.com/subject/jxjjdctx.html