cell上的button的点击事件

第一种方法:用tag值进行判断

第二种方法:block

在自定义cell里面

@property (weak, nonatomic) IBOutlet UIButton *btn;
@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;
}

第三种方法:代理

posted @ 2015-12-11 10:03  宋婷婷  阅读(855)  评论(0编辑  收藏  举报