先说需求,要做一个类似这种的列表

标签控件直接用的第三方

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卡顿优化的更多相关文章

  1. Android性能优化----卡顿优化

    前言 无论是启动,内存,布局等等这些优化,最终的目的就是为了应用不卡顿.应用的体验性好坏,最直观的表现就是应用的流畅程度,用户不知道什么启动优化,内存不足,等等,应用卡顿,那么这个应用就不行,被卸载的 ...

  2. android中app卡顿优化问题

     所谓app卡顿原因就是在运行时出现了丢帧,还可能是UI线程被阻塞.首先来一下丢帧现象,android每16ms会对界面进行一次渲染,如果app的绘制.计算等超过了16ms那么只能等下一个16ms才能 ...

  3. GC 卡顿 优化 三色标记优势

    小结: 1. 三色标记的一个明显好处是能够让用户程序和 mark 并发的进行 Go GC 卡顿由秒级降到毫秒级以下:到底做了哪些优化? https://mp.weixin.qq.com/s/2BMGG ...

  4. Android 卡顿优化 2 渲染优化

    1.概述 2015年初google发布了Android性能优化典范,发了16个小视频供大家欣赏,当时我也将其下载,通过微信公众号给大家推送了百度云的下载地址(地址在文末,ps:欢迎大家订阅公众号),那 ...

  5. Android 卡顿优化 1 卡顿解析

    1, 感知卡顿 用户对卡顿的感知, 主要来源于界面的刷新. 而界面的性能主要是依赖于设备的UI渲染性能. 如果我们的UI设计过于复杂, 或是实现不够好, 设备又不给力, 界面就会像卡住了一样, 给用户 ...

  6. 彻底解决 Intellij IDEA 卡顿 优化笔记,重要的快捷键

    由于工作中经常出现分支各种切换,使用Eclipse便不再像以前那么舒服了,不停的修改工作空间,每次修改完工作空间又是一堆一堆的个性化设置,来回的切换,真的很累.我们做软件的,怎么能不去尝试新鲜的呢,毕 ...

  7. 彻底解决 intellij IDEA 卡顿 优化笔记

    由于工作中经常出现分支各种切换,使用Eclipse便不再像以前那么舒服了,不停的修改工作空间,每次修改完工作空间又是一堆一堆的个性化设置,来回的切换,真的很累.我们做软件的,怎么能不去尝试新鲜的呢,毕 ...

  8. Android 布局渲染流程与卡顿优化

    文章内容概要 一.手机界面UI渲染显示流程 二.16ms原则 三.造成卡顿的原因 四.过度绘制介绍.检测工具.如何避免造成过度绘制造成的卡顿 一.手机界面UI渲染显示流程 大家都知道CPU(中央处理器 ...

  9. WPF DataGrid OxyPlot 卡顿优化

    不是优化,我是想用这个标题吸引遇到相同问题的同学过来看看. UI如下,左边DataGrid有7列,右边OxyPlot显示折线图 列表4000+数据,折线图4000+个点,页面卡的用不了. 体现就是列表 ...

随机推荐

  1. Bzoj1496: [NOI2006]千年虫

    题面 传送门 Sol 左右可以分开搞 然后就是要形成一个类似梳子的东西 设\(f[0/1][i][j]\) \(0\)凹,\(1\)凸,\(i\)为行,可以滚一维,\(j\)为该行长度 \(f[0][ ...

  2. 欣欣的留言板项目====超级触动的dbUtil实现留言板

    留言板管理系统 我的完成效果图: 提交后: 我的留言板基本架构如图: 创建留言板数据库: 刚开始我的前台主页中写留言信息表单: <body> <h1>留言板</h1> ...

  3. 三重for循环实现对二维数组的按列排序(JavaScript)

    由C语言联想到的:三重for循环实现对二维数组的按列排序,并且牵扯到数据结构. 自己写的,水平有限,本文属于原创,可能存在错误,忘指正~ function circle() { var a = [ [ ...

  4. 【Python】面向对象编程思想

    概念 "笔"作为一个抽象的概念,可以被看成是一个类.而一支实实在在的笔,则是"笔"这种类型的对象. 一个类可以有属于它的函数,这种函数被称为类的"方法 ...

  5. qt中qlineedit和qtextedit右键菜单翻译成中文

    没有linguist和lupdate等命令需要安装Linguist: 在Terminal中输入: sudo apt-get install qt4-dev-tools qt4-doc qt4-qtco ...

  6. 【Leetcode】【Medium】Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  7. angular2 遗留问题

    1.angular build [2017-07-26]  a.改写js/css的引用目录的前缀(比如统一增加 /abc/xxx/*.js)  b.build时,可以控制index/js/css的生成 ...

  8. [翻译]Elasticsearch重要文章之二:堆内存的大小和swapping

    Elasticsearch默认安装后设置的内存是1GB,对于任何一个业务部署来说,这个都太小了.如果你正在使用这些默认堆内存配置,你的集群配置可能有点问题. 这里有两种方式修改Elasticsearc ...

  9. Can't create new folder in windows7

    First, please use System File Checker tool to troubleshoot(诊断) this issue. If the issue persists, im ...

  10. python 函数&条件,循环

    条件if <条件判断1>: <执行1>elif <条件判断2>: <执行2>elif <条件判断3>: <执行3>else: & ...