iOS-UITableView的优化

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

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,998评论 3 38
  • TableView的优化 一:什么是TableView的优化以及为什么要优化 1)CPU(中央处理器)和GPU(图...
    学_iOS阅读 4,795评论 0 6
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,463评论 30 472
  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,980评论 1 180
  • 在学习稻盛和夫经营哲学中,让我深深反思的问题: 什么样的人是真正有实力的人? 拥有恪守职守的能力,同时人格高尚,值...
    静尚形象设计阅读 3,362评论 0 0

友情链接更多精彩内容