UITableView获取cell的indexPath

UITableView获取cell的indexPath

  (2014-02-20 15:03:58)
标签: 

iphone

 

ios

 

ipad

 

it

 
分类: ios-dev

假如你是用代码方式直接将控件(如UILabel、UIButton等)加到UITableView的cell中去的话,,,在出了

 

[cpp]  view plain copy
  1. (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2.  
  3.    //自定义代码  
  4.   return cell;   
  5.   

这个函数后,,,当你点击cell的时候想知道到底是点击了第几行,,这时候你就可以通过在以下代码获得点击的行数。

 

 

[cpp]  view plain copy
  1. UITableViewCell *cell (UITableViewCell *)[btn superview];  
  2. NSIndexPath *indexPath [_myTableView indexPathForCell:cell];  
  3. NSLog(@"indexPath is %i",indexPath.row);  

注释:btn是你通过代码的方式添加到cell的一个Button,_myTableView是UITableView的一个关联变量。

 


假如你是通过新建 .xib的方式新建一个继承UITableViewCell的 .xib(例如:shopCell.xib)文件添加到原有UITableView的cell的方式的话,,,用上面这种方法是获取不到点击cell所在的行数的,也就是说你不知道点击的cell到底是第几行。


同样可以用上面的代码,,不过要稍微修改一下:

 

[cpp]  view plain copy
  1. UITableViewCell *cell (UITableViewCell *)[[[btn superview] superview] superview];  
  2. NSIndexPath *indexPath [_myTableView indexPathForCell:cell];  
  3. NSLog(@"indexPath is %i",indexPath.row);  


解释:第一句代码中的[btn superview]是shopCell 的contentView,第二个superview是shopCell自己本身的cell,第三个superview是UITableView的cell,,注意不要弄混淆了。

 



20131024再次编辑:

知道row,然后通过row确定是哪一行的cell

 

[cpp]  view plain copy
  1. NSIndexPath *index1  [NSIndexPath indexPathForItem:row inSection:0];  
  2. UITableViewCell *cell1  [_myTableView cellForRowAtIndexPath:index1];  



转自:http://blog.csdn.net/chaoyuan899/article/details/12972131
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值