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

在使用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.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值