在自定义cell里面
@property (nonatomic, strong) void ((^btnClick)());
- (void)btn:(UIButton *)sender{
if (self.btnClick) {
self.btnClick();
}
}
- (void)awakeFromNib {
[_btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
}
在controller里面
#pragma mark ----- tabelView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FirstTableViewCell *cell = (FirstTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FirstCellReuse"];
if (cell == nil) {
NSArray * nib = [[NSBundle mainBundle]loadNibNamed:@"FirstTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = self.array[indexPath.row];
cell.btnClick = ^(){
NSLog(@"%ld",(long)indexPath.row);
};
return cell;
}