记一次简单的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+个点,页面卡的用不了. 体现就是列表 ...
随机推荐
- 51nod 1135 原根(原根)
题意 题目链接 Sol 可以证明素数的原根不会超过他的\(\frac{1}{4}\) 那么预处理出\(P - 1\)的所有的质因数\(p_1, p_2 \dots p_k\),暴力判断一下,如果$\e ...
- JavaScript 二维数组排列组合2
<html> <head> <title>二维数组排列组合</title> </head> <body> <div id= ...
- cnpm 安装
国内npm 安装比较慢,可选择cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org
- 建立makefile
一.目的 1.1 熟悉makefile的基本规则,学会建立自己的makefile,通过makefile提高编程效率 1.2 学会通过autotools建立makefile来更好更高效地管理软件项目工程 ...
- Mac下终端自动补全功能
记录一下终端的一些使用命令,跟自动补全的配置,主要怕以后忘记了. 1.终端自动补全的配置 打开终端,输入 : nano .inputrc 在文件里面写上: set completion-ignore- ...
- 使用Axure管理团队项目以及分享原型
第一部分:使用Axure管理团队项目 首先,你要有一个Axure账户 呵呵哒.注册地址:http://share.axure.com 发起团队项目 在浏览器登录 Axure share(网速很卡很卡) ...
- css display属性详解
css display属性在对css做layout设计时非常重要,它的值有以下几种: Value Description Play it inline Default value. Displays ...
- 【Leetcode】【Medium】Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- CI框架, 参数验证
/** * 统一API参数检验方法 * * 调用示例 check_param(array('money' => array('required', 'integer', 'greater_tha ...
- Deep Learning Drizzle
Deep Learning Drizzle Drench yourself in Deep Learning, Reinforcement Learning, Machine Learning, Co ...