iOS UITableView获取cell的indexPath及cell内部按钮点击事件处理

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

static NSString *cellID = @"test";

testCell *cell = (testCell *)[tableView dequeueReusableCellWithIdentifier:cellID];

if (!cell) {
    cell = [[[NSBundle mainBundle]loadNibNamed:@"testCell" owner:self options:nil]lastObject];
}

cell.testLabel.text = self.data[indexPath.row];
//添加按钮点击事件
[cell.testBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

return cell;
}

 

//按钮点击事件
-(void)click:(UIButton *)btn{

UIView *contentView = [btn superview];
testCell *cell = (testCell *)[contentView superview];
NSIndexPath *indexPath = [self.tb indexPathForCell:cell];

NSLog(@"%@----%@",indexPath,self.data[indexPath.row]);
}

 

posted @ 2021-10-20 10:11  张亚楠  阅读(542)  评论(0编辑  收藏  举报