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

记一次简单的UITableView卡顿优化-LMLPHP

标签控件直接用的第三方

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.当然了,我的标题顺序并不是代码运行顺序。

05-11 21:56