关于UITableView的一些性能优化
在实际iOS开发中,我们用的最多和最常见的一个UI空间就是列表(UITableView),在UI空间中设计到的知识点也是列表最多,比如列表的一系列协议方法,包括必须实现的和可选实现的。那么在实际开发中,为了给用户一个更好的体验,那么久需要进行性能的一下改善。
UITableView的介绍
UITableView是继承于UIScrollview的,因此可以自动响应滚动事件,UITableView最常用的两个协议是:UITableViewDataSource和UITableViewDelegate,对于这两个协议里面的常用方法:
UITableViewDataSource的常用方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@optional
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
在实际开发中,我们需要注意的是创建TableView时,尽可能的用懒加载的创建方式,这样的话对内存占用会有明显的减少,示例代码:
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_tableView registerNib:[UINib nibWithNibName:@"QLTableViewCell" bundle:nil] forCellReuseIdentifier:@"tableViewCell"];
[_tableView registerNib:[UINib nibWithNibName:@"QLheaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"tableHeaderView"];
_tableView.backgroundColor = [UIColor clearColor];
}
return _tableView;
}
当然在注册cell的时候,上面是直接给了一个Identifier,其实为了更好的优化,建议大家这么写:
static NSString *Identifier = @"QLTableViewCell";然后在注册的时候可以直接调用。
数据源数组,尽可能的也用懒加载,在数据请求回来后,进行self.dataArray进行保存。
如果你在开发的时候对于Cell,用的是xib,那么你在这里也可以优化,
1、尽可能的避免系统反复调用
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
2、在创建cell的时候,避免重复创建,可以利用复用:
pragma mark - 协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell" forIndexPath:indexPath];
//去掉cell选中的背景颜色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.model = self.dataArray[indexPath.row];
return cell;
}
3、不使用1进行设置行高,利用系统提供的属性,进行自动缓存和计算行高,并进行展示:
/*估算tableView的高度为200*/
self.tableView.estimatedRowHeight = 200;
/*自动计算行高*/
self.tableView.rowHeight = UITableViewAutomaticDimension;
4、模型和视图进行分开,在进行VC的关联,实现MVC开发模式。
在请求数据的时候,异步请求,刷新视图的数据的时候回到主线程刷新,实现高内聚,低耦合。