iOS-UITableView的优化

作者: 长风留言 | 来源:发表于2018-03-06 16:00 被阅读13次

关于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开发模式。

在请求数据的时候,异步请求,刷新视图的数据的时候回到主线程刷新,实现高内聚,低耦合。

相关文章

  • iOS-UITableView的优化

    关于UITableView的一些性能优化 在实际iOS开发中,我们用的最多和最常见的一个UI空间就是列表(UITa...

  • iOS-UITableView的优化

    TableView的优化 一:什么是TableView的优化以及为什么要优化 1)CPU(中央处理器)和GPU(图...

  • iOS-UITableView性能优化

    1.最常用的就是cell的重用, 注册重用标识符它的原理是,根据cell高度和tableView大小,确定界面上能...

  • iOS-UITableView性能优化

    UITableView性能优化,这个问题只要做iOS研发相关工作的人都会遇到,或是工作开发需要,或者面试问题。 我...

  • iOS-UITableView重用机制和性能优化、

    简介: UITableView我想大家都不陌生,他是UIKit一个重要组件。可以用来展示数据列表,或者灵活使用进行...

  • iOS-UITableView 详解(一)

    iOS-UITableView 详解 (一) ✨建议收藏,用到时候一查就明白了UITableView可以说是iOS...

  • iOS-UITableView重用机制和性能优化、XXX

    简介: UITableView我想大家都不陌生,他是UIKit一个重要组件。可以用来展示数据列表,或者灵活使用进行...

  • iOS-UITableView

    文章索引01 tableView 属性02 tableView 方法03 tableView 动画块操作04...

  • iOS-UITableView、UICollectionView

    本篇涵盖UITableView、UICollectionView的缓存、计算、动画等. 1.使用UITableVi...

  • IOS-UITableView简述

    UITableView 在APP中各处都在使用UITableView,必须熟记,今天简单说明UITableView...

网友评论

    本文标题:iOS-UITableView的优化

    本文链接:https://www.haomeiwen.com/subject/uicrfftx.html