ios tableview点击监听 alertview传值技巧

实现监听,使用tableview代理

代理方法:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  2.    NSLog(@"选中了第%d组的第%d行",indexPath.section,indexPath.row);}  
  3.    
  4. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{  
  5.    NSLog(@"取消选中了第%d组的第%d行",indexPath.section,indexPath.row);}  
细节:在两个方法之间传值得方法,tableview的点击事件触发了Alertview,此时得到了indexPath的,但是当Alertview被点击触发委托函数时,无法得到indexpath,这种传值不宜用全局变量,可以使用Alertview的tag来传这个值。

 alertView.tag = indexPath.row;

实现alertview的监听方法

 (void)alertView:(UIAlertView *)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex{  
    //点击了取消  
    if (buttonIndex == 0) return;  
}  
修改数据或者相应的操作。

一个问题:reloadData刷新会将视野内的所有cell全部刷新,造成了内存浪费,这个用于刷新内容多的时候。

可以使用局部刷新来解决这个问题:只重新加载选中的cell

局部刷新还可以使用动画:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];  
  2. [self.tableview reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];  


注意要传入刷新的组的数组的IndexPath,事先创建一个indexPath再传入,后面的值还可以设定动画。

 

为什么不直接改cell因为cell的数据来自模型,模型不改,cell再次刷新还会变回模型的数据




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值