记一次简单的UITableView卡顿优化
先说需求,要做一个类似这种的列表
标签控件直接用的第三方
YZTagList
不知道的可以去搜一下,当这不重要。
重要的是这个控件加载数据的时候非常影响列表滑动效果,造成卡顿,尤其是列表行数如果更多的话,
这也不是要说的重点,自己写的控件也不一定就不耗性能,所以记一下我这次的处理方法。
先看一下cell代码:
@interface PreferTableViewCell ()
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, strong) YZTagList *tagList;
@end @implementation PreferTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self setupUI];
}
return self;
}
- (void)setupUI {
UIView *lineView = [[UIView alloc] init];
lineView.backgroundColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:1.0];
[self.contentView addSubview:lineView];
[lineView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self);
make.right.equalTo(self);
make.height.equalTo();
make.top.equalTo(self);
}]; UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont systemFontOfSize:];
[self.contentView addSubview:titleLabel];
self.titleLabel = titleLabel;
[titleLabel makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset();
make.top.equalTo(lineView.bottom).offset();
}]; YZTagList *tagList = [[YZTagList alloc] init];
tagList.isSort = NO;
tagList.backgroundColor = [UIColor whiteColor];
// 高度可以设置为0,会自动跟随标题计算
tagList.frame = CGRectMake(, , SCREEN_WIDTH - , );
// 设置标签背景色
tagList.tagBackgroundColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:1.0];
// 设置标签颜色
tagList.tagColor = [UIColor colorWithHex:@"#303030"]; tagList.tagFont = [UIFont systemFontOfSize:];
tagList.tagCornerRadius = ;
tagList.clickTagBlock = ^(UIButton *btn){
btn.selected = !btn.isSelected;
if (btn.isSelected) {
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithHex:@"#7676FD"]];
} else {
[btn setTitleColor:[UIColor colorWithHex:@"#303030"] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:1.0]];
}
};
[self.contentView addSubview:tagList]; self.tagList = tagList;
}
- (void)setTitle:(NSString *)title {
_title = title;
self.titleLabel.text = title;
}
- (void)setTags:(NSArray *)tags {
_tags = tags; if (self.tagList.tagArray.count > ) {
return;
}
[self.tagList addTags:tags];
}
+ (CGFloat)calcHeight:(NSArray *)tags {
YZTagList *tagList = [[YZTagList alloc] init];
tagList.frame = CGRectMake(, , SCREEN_WIDTH - , );
tagList.tagFont = [UIFont systemFontOfSize:];
tagList.tagCornerRadius = ;
[tagList addTags:tags];
return tagList.tagListH + + ;
}
@end
然后是VC代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.tags.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *ID = [NSString stringWithFormat:@"%ld%ld",indexPath.section,indexPath.row];
PreferTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[PreferTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.title = @"";
cell.tags = self.tags[indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = ;
height = [[self.cellheightCache objectForKey:[NSString stringWithFormat:@"%ld%ld",indexPath.section,indexPath.row]] floatValue];
if (height == ) {
height = [PreferTableViewCell calcHeight:self.tags[indexPath.row]];
[self.cellheightCache setObject:@(height) forKey:[NSString stringWithFormat:@"%ld%ld",indexPath.section,indexPath.row]];
}
return height;
}
前提:tags是一个二维数组。
1.首先没有使用cell重用机制,避免多次
[self.tagList addTags:tags];
影响滑动效果,反正标签占用内存微乎其微。
然后:
if (self.tagList.tagArray.count > 0) { return; }
一样的目的。到这里每个cell赋值只会发生一次。并且cell视图均缓存在内存中了。
2.然后是计算行高,计算行高需要用到一个类方法,代码写的很清楚了,直接用
YZTagList
算出来并返回。然后用一个可变字典将高度缓存起来,这样保证高度也只会计算一次。
3.当然了,我的标题顺序并不是代码运行顺序。
记一次简单的UITableView卡顿优化的更多相关文章
- Android性能优化----卡顿优化
前言 无论是启动,内存,布局等等这些优化,最终的目的就是为了应用不卡顿.应用的体验性好坏,最直观的表现就是应用的流畅程度,用户不知道什么启动优化,内存不足,等等,应用卡顿,那么这个应用就不行,被卸载的 ...
- android中app卡顿优化问题
所谓app卡顿原因就是在运行时出现了丢帧,还可能是UI线程被阻塞.首先来一下丢帧现象,android每16ms会对界面进行一次渲染,如果app的绘制.计算等超过了16ms那么只能等下一个16ms才能 ...
- GC 卡顿 优化 三色标记优势
小结: 1. 三色标记的一个明显好处是能够让用户程序和 mark 并发的进行 Go GC 卡顿由秒级降到毫秒级以下:到底做了哪些优化? https://mp.weixin.qq.com/s/2BMGG ...
- Android 卡顿优化 2 渲染优化
1.概述 2015年初google发布了Android性能优化典范,发了16个小视频供大家欣赏,当时我也将其下载,通过微信公众号给大家推送了百度云的下载地址(地址在文末,ps:欢迎大家订阅公众号),那 ...
- Android 卡顿优化 1 卡顿解析
1, 感知卡顿 用户对卡顿的感知, 主要来源于界面的刷新. 而界面的性能主要是依赖于设备的UI渲染性能. 如果我们的UI设计过于复杂, 或是实现不够好, 设备又不给力, 界面就会像卡住了一样, 给用户 ...
- 彻底解决 Intellij IDEA 卡顿 优化笔记,重要的快捷键
由于工作中经常出现分支各种切换,使用Eclipse便不再像以前那么舒服了,不停的修改工作空间,每次修改完工作空间又是一堆一堆的个性化设置,来回的切换,真的很累.我们做软件的,怎么能不去尝试新鲜的呢,毕 ...
- 彻底解决 intellij IDEA 卡顿 优化笔记
由于工作中经常出现分支各种切换,使用Eclipse便不再像以前那么舒服了,不停的修改工作空间,每次修改完工作空间又是一堆一堆的个性化设置,来回的切换,真的很累.我们做软件的,怎么能不去尝试新鲜的呢,毕 ...
- Android 布局渲染流程与卡顿优化
文章内容概要 一.手机界面UI渲染显示流程 二.16ms原则 三.造成卡顿的原因 四.过度绘制介绍.检测工具.如何避免造成过度绘制造成的卡顿 一.手机界面UI渲染显示流程 大家都知道CPU(中央处理器 ...
- WPF DataGrid OxyPlot 卡顿优化
不是优化,我是想用这个标题吸引遇到相同问题的同学过来看看. UI如下,左边DataGrid有7列,右边OxyPlot显示折线图 列表4000+数据,折线图4000+个点,页面卡的用不了. 体现就是列表 ...
随机推荐
- thinkphp5设置404页面不跳转
thinkphp5设置404页面的步骤: 1. 首先关闭调试模式,即配置application/config文件,使'app_debug' => false 2. 添加自定义404页面跳转地址, ...
- HTML5拨号 调用手机拨号功能
<a href="tel:123456789">拨号</a> 这个就是HTML5 运行在手机浏览器上的可以调用手机的拨号tel就是你想要拨打的电话号码
- Activiti 配置Oracle不能自动创建表解决方法
使用配置文件创建工作流表 <bean id="processEngineConfiguration" class="org.activiti.engine.impl ...
- eclipse svn使用
简单介绍一些基本操作 1.同步在Eclipse下,右击你要同步的工程->team->与资源库同步->这时会进入同步透视图,会显示出本机与SVN上内容有不同的文件,双击文件名,会显示出 ...
- WPF 蒙罩层 LoadingPage
WPF 蒙罩层 LoadingPage 前言 无论是在PC客户端,移动端,网站,在遇到长时间处理的时候都会需要用到蒙罩层,让用户有更好的体现.今天上网逛了一下各位前辈网友的蒙罩层的实现方式,觉得有很多 ...
- Python爬虫教程-20-xml 简介
本篇简单介绍 xml 在python爬虫方面的使用,想要具体学习 xml 可以到 w3school 查看 xml 文档 xml 文档链接:http://www.w3school.com.cn/xmld ...
- qt 创建资源文件
我们编写的gui可能需要一些额外的资源(比如贴图用的图片),可用资源文件统一管理.以下以图片为例. 用qt creator 打开工程,为工程新建资源文件: 命名为“项目名.prc”,(此处为“cloc ...
- linux crontab 的使用
linux crontab 的使用 准备(实验楼需要,实际环境不需要):sudo service rsyslog startsudo cron -f & crontab 使用添加任务:cron ...
- 【Leetcode】【Medium】Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- c++ auto_ptr超简易版实现
namespace wang{ template<class T> class shared_ptr{ public: explicit shared_ptr(T *p) : count( ...