ios 获取当前或指定的cell

这篇博客介绍了如何在iOS应用中为表格视图(UITableView)的单元格(Cell)设置代理方法,以便在点击按钮时能够获取到当前或指定的Cell。通过实现点击事件的回调,可以获取到触发事件的indexPath,并使用tableView的cellForRowAtIndexPath方法来获取对应的 UITableViewCell。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们就给cell一个代理方法

[objc]  view plain  copy
  1. // 点击按钮  
  2. - (IBAction)click:(UIButton *)sender {  
  3.       
  4.     if ([self.delegate respondsToSelector:@selector(MKJTableView:clickButton:)]) {  
  5.         [self.delegate MKJTableView:self clickButton:sender];  
  6.     }  
  7. }  
让Controller实现这个代理

[objc]  view plain  copy
  1. // cell的代理方法  
  2. - (void)MKJTableView:(MKJTableViewCell *)cell clickButton:(UIButton *)touchBtn  
  3. {  
  4.      
  5. }  



  1. // 这里的2和0可以根据需要求更改 这里就是第0段,第2行  
  2. NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:2 inSection:0];  
  3. UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值