在使用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 |
插入的时候刷新 |
总结:
1.利用缓存机制初始化cell通过tableView dequeueReusableCellWithIdentifier:ID获取,如果缓存中没有则通过[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:ID创建。
2.开启Tableview的编辑模式,[self.tableView setEditing:YES animated:YES],而开启编辑模式的时候会调用editingStyleForRowAtIndexPath返回当前的编辑状态。
3.提交编辑操作的时候调用commitEditingStyle
4.排序功能实现moveRowAtIndexPath方法即可
原文:http://blog.csdn.net/cooljune/article/details/19246555