美文网首页
多个cell上按钮的点击事件

多个cell上按钮的点击事件

作者: 灵姝斐 | 来源:发表于2017-09-15 15:58 被阅读0次
当多个cell可滑动,且每个cell中有一个button,点击这个button的时候需要知道当前点击的是哪一个cell上的按钮.
解决方式:
  1. 自定义一个cell,继承与UITableViewCell,并为这个cell创建一个协议如下,协议方法中将自己当作参数使用,如下:
@property (nonatomic, weak) id< CustomTableViewCellDelegate >delegate;

@protocol CustomTableViewCellDelegate

- (void)customTableViewCell:(CustomTableViewCell *)cell;

@end
  1. 然后在你自定义的cell 类中创建的 button 控件的方法中实现
- (void)pressButton:(UIButton *)sender {

if ([self.delegate respondsToSelector:@selector(customTableViewCell:)]) {
      [self.delegate customTableViewCell:self];
}
}
  1. 最后在你需要实现协议的控制器中,调用便可以了, (别忘记控制器要遵守协议,谁遵守协议谁就来实现协议中的方法)
- (void)customTableViewCell:(CustomTableViewCell *)cell{

   NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

}

相关文章

网友评论

      本文标题:多个cell上按钮的点击事件

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