ios tableview点击监听 alertview传值技巧

来源:互联网 发布:淘宝怎么改收获地址 编辑:程序博客网 时间:2024/06/08 06:11

实现监听,使用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再次刷新还会变回模型的数据




0 0
原创粉丝点击