分享

UITableView的基本使用二(性能优化)

 ccccshq 2014-08-08

在使用UITableView的时候,会有很多的cell数据产生,如果数据量很大,而且用户在界面上操作频繁的时候,就会造成性能下降,那么这个时候我们要考虑使用缓存机制,也就是像Java中的缓存机制一样,用过Memcache或者使用过数据库连接池的同学肯定知道这个原理,如果缓存池中有就用缓存池中的,如果没有再创建。而在操作cell的时候,比如删除添加修改的时候,都要遵循MVC模式,通过修改数据来修改cell的UI。


  1. @interface cooljuneViewController ()  
  2.   
  3. @property(nonatomic,strongNSMutableArray *data;  
  4.   
  5. @end  
  6.   
  7. @implementation cooljuneViewController  
  8.   
  9. - (void)viewDidLoad  
  10. {  
  11.     [super viewDidLoad];  
  12.       
  13.     self.data=[NSMutableArray array];  
  14.       
  15.     for (int i=0; i<30; i++) {  
  16.         NSString *text=[NSString stringWithFormat:@"str%d",i];  
  17.         [self.data addObject:text];  
  18.     }  
  19.       
  20.       
  21. }  
  22.   
  23. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  24.       
  25.     return self.data.count;  
  26.       
  27. }  
  28.   
  29. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  30.     static NSString *ID=@"Cell";  
  31.     //从缓存中获取  
  32.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];  
  33.     if (cell==nil) {  
  34.         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:ID];  
  35.     }  
  36.     cell.textLabel.text=self.data[indexPath.row];  
  37.     return cell;  
  38. }  
  39.   
  40.   
  41. //提交编辑操作时候调用 点击添加或者删除  
  42. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{  
  43.       
  44.       
  45.     if (editingStyle==UITableViewCellEditingStyleDelete) {  
  46.         //更改数据  
  47.           
  48.         [self.data removeObjectAtIndex:indexPath.row];  
  49.           
  50.         //刷新指定行,也就是删除指定cell  
  51.         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];  
  52.     }else{  
  53.       
  54.         NSString *text=@"aaaaaa";  
  55.         //[self.data addObject:@""];  
  56.         [self.data insertObject:text atIndex:indexPath.row+1];  
  57.         //[tableView reloadData];  
  58.           
  59.         //刷新指定行(个数不变)  
  60.         //[tableView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>];  
  61.           
  62.         //指定动画位置  
  63.         //调用cellForRowAtIndexPath  
  64.         NSIndexPath *path=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];  
  65.           
  66.         //插入的时候刷新  
  67.         [tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationFade];  
  68.     }  
  69.       
  70.       
  71.   
  72. }  
  73.   
  74. //开启编辑模式的时候调用 可视范围内有多少行就会调用多少次  
  75. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{  
  76.     NSLog(@"aaa");  
  77.     return tableView.tag;  
  78. }  
  79.   
  80. -(IBAction)removeRow{  
  81.      
  82.      
  83.     self.tableView.tag=UITableViewCellEditingStyleDelete;  
  84.      BOOL flag=self.tableView.editing;  
  85.     //设置编辑模式  
  86.     [self.tableView setEditing:!flag animated:YES];  
  87. }  
  88.   
  89. -(void)addRow{  
  90.       
  91.     self.tableView.tag=UITableViewCellEditingStyleInsert;  
  92.     BOOL flag=self.tableView.editing;  
  93.     [self.tableView setEditing:!flag animated:YES];  
  94.       
  95. }  
  96.   
  97. //排序功能  
  98. -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{  
  99.     //取出要删除的数据  
  100.     NSString *text=self.data[sourceIndexPath.row];  
  101.     //删除数据  
  102.     [self.data removeObjectAtIndex:sourceIndexPath.row];  
  103.     //插入shuj  
  104.     [self.data insertObject:text atIndex:destinationIndexPath.row];  
  105.   
  106. }  
  107.   
  108. @end  

刷新cell的几种方式:

tableView reloadData

全局刷新,性能底

tableView reloadRowsAtIndexPaths

刷新指定行,数据个数不发生改变

tableView deleteRowsAtIndexPaths

删除的时候刷新

insertRowsAtIndexPaths

插入的时候刷新

常用属性

self.tableView setSeparatorColor:[UIColor redColor];

设置分割线颜色

self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLineEtched;

设置分割线样式

self.tableView.allowsMultipleSelection

是否允许多选

self.tableView.allowsSelection=YES;

是否相应点击操作

self.tableView indexPathsForSelectedRows

返回选中的多行

self.tableView indexPathsForVisibleRows

可见的行

cell.selectedBackgroundView

cell.backgroundView

选中背景色

默认背景色

总结:

1.利用缓存机制初始化cell通过tableViewdequeueReusableCellWithIdentifier:ID获取,如果缓存中没有则通过[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue2reuseIdentifier:ID创建。

2.开启Tableview的编辑模式,[self.tableViewsetEditing:YESanimated:YES],而开启编辑模式的时候会调用editingStyleForRowAtIndexPath返回当前的编辑状态。

3.提交编辑操作的时候调用commitEditingStyle

4.排序功能实现moveRowAtIndexPath方法即可

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多