UITableView的基本使用二(性能优化)
2014-02-15 17:35
495 查看
在使用UITableView的时候,会有很多的cell数据产生,如果数据量很大,而且用户在界面上操作频繁的时候,就会造成性能下降,那么这个时候我们要考虑使用缓存机制,也就是像Java中的缓存机制一样,用过Memcache或者使用过数据库连接池的同学肯定知道这个原理,如果缓存池中有就用缓存池中的,如果没有再创建。而在操作cell的时候,比如删除添加修改的时候,都要遵循MVC模式,通过修改数据来修改cell的UI。
刷新cell的几种方式:
常用属性
总结:
1.利用缓存机制初始化cell通过tableViewdequeueReusableCellWithIdentifier:ID获取,如果缓存中没有则通过[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue2reuseIdentifier:ID创建。
2.开启Tableview的编辑模式,[self.tableViewsetEditing:YESanimated:YES],而开启编辑模式的时候会调用editingStyleForRowAtIndexPath返回当前的编辑状态。
3.提交编辑操作的时候调用commitEditingStyle
4.排序功能实现moveRowAtIndexPath方法即可
@interface cooljuneViewController () @property(nonatomic,strong) NSMutableArray *data; @end @implementation cooljuneViewController - (void)viewDidLoad { [super viewDidLoad]; self.data=[NSMutableArray array]; for (int i=0; i<30; i++) { NSString *text=[NSString stringWithFormat:@"str%d",i]; [self.data addObject:text]; } } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.data.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID=@"Cell"; //从缓存中获取 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:ID]; } cell.textLabel.text=self.data[indexPath.row]; return cell; } //提交编辑操作时候调用 点击添加或者删除 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle==UITableViewCellEditingStyleDelete) { //更改数据 [self.data removeObjectAtIndex:indexPath.row]; //刷新指定行,也就是删除指定cell [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; }else{ NSString *text=@"aaaaaa"; //[self.data addObject:@""]; [self.data insertObject:text atIndex:indexPath.row+1]; //[tableView reloadData]; //刷新指定行(个数不变) //[tableView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]; //指定动画位置 //调用cellForRowAtIndexPath NSIndexPath *path=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]; //插入的时候刷新 [tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationFade]; } } //开启编辑模式的时候调用 可视范围内有多少行就会调用多少次 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"aaa"); return tableView.tag; } -(IBAction)removeRow{ self.tableView.tag=UITableViewCellEditingStyleDelete; BOOL flag=self.tableView.editing; //设置编辑模式 [self.tableView setEditing:!flag animated:YES]; } -(void)addRow{ self.tableView.tag=UITableViewCellEditingStyleInsert; BOOL flag=self.tableView.editing; [self.tableView setEditing:!flag animated:YES]; } //排序功能 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ //取出要删除的数据 NSString *text=self.data[sourceIndexPath.row]; //删除数据 [self.data removeObjectAtIndex:sourceIndexPath.row]; //插入shuj [self.data insertObject:text atIndex:destinationIndexPath.row]; } @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方法即可
相关文章推荐
- iOS开发中UITableview控件的基本使用及性能优化方法
- iOS 之 UITableView 使用详解/性能优化/UITableViewCell/UITableViewController
- UI 一一 UITableView的使用和性能优化
- UITableView使用总结和性能优化
- 性能优化-UITableView的优化使用
- 【iOS 技巧 - 3 】uitableView cell基本的性能优化
- UITableView性能优化及使用总结
- UITableView 基本使用方法总结
- 【解决方法】UITableView 性能优化笔记
- Xcode - 使用 DWURecyclingAlert 进行 UITableView 和 UICollectionView 的绘图性能测试https://viktyz.gitbooks.io/ios
- 使用AsyncDisplayKit提升UICollectionView和UITableView的滚动性能
- ios--UITableview的基本使用方法
- UITableView 基本使用方法总结
- UI—UITableView的性能优化、plist文件的加载
- UITableView 基本使用方法总结
- iOS开发——项目实战总结&UITableView性能优化与卡顿问题
- Android-优化UI性能(4)-使用ViewStub
- UITableView性能优化的几种方式