iOS UITableView之cellForRowAtIndexPath和indexpathForCell分别获取指定cell和indexpath

转自:http://blog.csdn.net/deft_mkjing/article/details/51564422


如何通过UITableView的cell获取对应的indexpath,或者通过indexpath获取对应的cell

简单介绍下这两个方法

1.

[plain]  view plain  copy
  1. - (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;  
  2. // 官网描述  
  3. Returns an index path representing the row and section of a given table-view cell.  
  4. An index path representing the row and section of the cell, or nil if the index path is invalid.  
  5. 返回值是indexpath类型,代表了给出那个cell的section和row,如果cell=nil,indexpath是无效的  

    2.
[plain]  view plain  copy
  1. - (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;  
  2. Returns the table cell at the specified index path.  
  3. An object representing a cell of the table, or nil if the cell is not visible or indexPath is out of range.  
  4. 返回指定indexpath下对应的cell,代表了一个tableView里面的cell对象,当cell不可见或者indexpath越界了,返回nil  


一般来说,常规的tableView的两个代理方法里面进行操作

[objc]  view plain  copy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     MKJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identyfy forIndexPath:indexPath];  
  4.     // 布局cell  
  5.     return cell;  
  6. }  


[objc]  view plain  copy
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];  
  4.     // 点击cell的时候直接能获取到indexpath  
  5.     NSLog(@"点击整个cell%@",indexPath);  
  6. }  

1.第一个方法案例

还有一种情况大家应该很常见了,就是每个cell里面有个按钮,暂时就做关注按钮吧,如何知道点击那个按钮的时候,是点击了哪个cell?

先来个图,就是点这个



我是这么做的

首先:布局UI问题不大吧,然后你把这个按钮的点击事件拖到你的cell.m文件里面,由于我是要到我的Controller进行下一波操作

然后:我们就给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. }  

之后:我们点击按钮,就会回调到这个方法里面进行下一波操作了 QWER随你来了


其实已经很明确了,一般设置代理都会把自身作为参数传过来

那么我们的第一种最直观的方法就是直接获取啊

[objc]  view plain  copy
  1. NSIndexPath *indexpath1 = [self.tableView indexPathForCell:cell];  
  2.     NSLog(@"第一种简单粗暴%@",indexpath1);  

如果脑残了没有把cell传过来,也没事,看看第二种方法获取

[objc]  view plain  copy
  1. MKJTableViewCell *cell1 = (MKJTableViewCell *)[[touchBtn superview] superview];  
  2. NSIndexPath *indexpath2 = [self.tableView indexPathForCell:cell1];  
  3. NSLog(@"第二种方法%@",indexpath2);  
这里的第一个superView指的是Cell的contentView,第二个superView不就是获取到了cell么


反正能拿到cell了,那么下边的操作就可以随意赋值操作了


看下具体实现,注意看打印



github地址:demo地址,需要的请点击


第二个方法案例

如图所示,根据Picker选择的地址,给指定indexpath的cell赋值

NOTE:这个picker三级联动的Demo会近期写成博客记录下来,如果有看的人或者留言的人会尽快写的



那么我们首选需要根据indexpath获取到cell,代码如下

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

获取到Cell之后再进行赋值就很简单了


安静安静安静Over~~~~~~~~~~~~~~~~~~~~~~




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值