先说需求,要做一个类似这种的列表
标签控件直接用的第三方
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;
}