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方法即可

0 0
原创粉丝点击