UITableView的基本使用二(性能优化)
来源:互联网 发布:mac cad中文版 编辑:程序博客网 时间:2024/06/05 23:46
在使用UITableView的时候,会有很多的cell数据产生,如果数据量很大,而且用户在界面上操作频繁的时候,就会造成性能下降,那么这个时候我们要考虑使用缓存机制,也就是像Java中的缓存机制一样,用过Memcache或者使用过数据库连接池的同学肯定知道这个原理,如果缓存池中有就用缓存池中的,如果没有再创建。而在操作cell的时候,比如删除添加修改的时候,都要遵循MVC模式,通过修改数据来修改cell的UI。
@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.tableViewsetSeparatorColor:[UIColorredColor];
设置分割线颜色
self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLineEtched;
设置分割线样式
self.tableView.allowsMultipleSelection
是否允许多选
self.tableView.allowsSelection=YES;
是否相应点击操作
self.tableViewindexPathsForSelectedRows
返回选中的多行
self.tableViewindexPathsForVisibleRows
可见的行
cell.selectedBackgroundView
cell.backgroundView
选中背景色
默认背景色总结:
1.利用缓存机制初始化cell通过tableViewdequeueReusableCellWithIdentifier:ID获取,如果缓存中没有则通过[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue2reuseIdentifier:ID创建。
2.开启Tableview的编辑模式,[self.tableViewsetEditing:YESanimated:YES],而开启编辑模式的时候会调用editingStyleForRowAtIndexPath返回当前的编辑状态。
3.提交编辑操作的时候调用commitEditingStyle
4.排序功能实现moveRowAtIndexPath方法即可
- UITableView的基本使用二(性能优化)
- UITableView的基本使用(二)
- 性能优化-UITableView的优化使用
- UITableView性能优化(复用池的使用原理)
- UITableView基本使用(二)
- 优化UITableView的性能
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UItableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化
- UITableView的性能优化问题
- 关于UITableView的性能优化
- 贪心与标记
- WPF之数据绑定总结
- SSH下载方法实现
- 学习bash shell 3
- UVa 10014 简单的计算
- UITableView的基本使用二(性能优化)
- 基础加强____【Java类加载器 & "委托机制"】
- 小P的故事——神奇的Dota
- 快速排序的层次改进
- 高手速成android开源项目【developer篇】
- max函数比较字符串类型
- 哈希表学习实例
- innotop 下载安装步骤
- 黑马程序员-JAVA高级(IO输入与输出)PART3