美文网首页
iOS-UITableViewCell的一些事

iOS-UITableViewCell的一些事

作者: 最强的小强 | 来源:发表于2019-01-25 15:20 被阅读0次
Q1:点击自己的UITableViewCell,cell上面的UILabel的背景会隐藏,下次又出现的问题?

A1: 如果自己所写的tableViewCell 上面的UILabel你需要设置圆角和背景,那么他就会影藏起来,那么你需要在-(void)layoutSubViews方法中重新设置一遍(小编亲测好用)
接下来直接上code

-(void)setDescLabel:(UILabel *)descLabel {
    _descLabel = descLabel;
    _descLabel.textColor = RGB(255, 255, 255, 1);
    _descLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size: 10];
    _descLabel.textAlignment = NSTextAlignmentCenter;
    _descLabel.layer.backgroundColor = RGB(51, 51, 51, 1).CGColor;
    _descLabel.layer.cornerRadius = 10.0;
    _descLabel.layer.masksToBounds = YES;
}
// 解决UILabel背景消失
- (void)layoutSubviews
{
    [super layoutSubviews];
    _descLabel.layer.backgroundColor = RGB(51, 51, 51, 1).CGColor;
    _descLabel.layer.cornerRadius = 10.0;
    _descLabel.layer.masksToBounds = YES;
}
Q2:xib只适用于在静态页面中使用;如果在普通页面中使用,而且你还想设置自定义cell的宽度和圆角等等,就必须是重写-(void)setFrame:(CGRect)frame这个方法,但是这个方法会影响到你滑动删除的功能(谨慎使用,如果你的cell没有用到滑动删除,那当然就不用考虑到这个问题了)?

A2: 针对xib,小编目前也没有好的解决方法。所以我的解决方法是在普通页面不去使用xib,用纯代码自定义cell,不去重写-(void)layoutSubViews这个方法,在cell上面添加一层背景View,这样就能完美的解决所提出的问题了(小编亲测好用)
接下来直接上code


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 点击没有颜色改变
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        self.backgroundColor = [UIColor clearColor];
        
        self.bgView2 = [UIView new];
        self.bgView2.layer.cornerRadius = 5;
        self.bgView2.backgroundColor = [UIColor whiteColor];
        _bgView2.frame = CGRectMake(10, 3, (Screen_Width-2*10), 67*Height_Ratio);
        [self.contentView addSubview: self.bgView2];
        
        self.nameLabel = [UILabel new];
        self.nameLabel.frame = CGRectMake(leftMargin*2, leftMargin, (Screen_Width-2*leftMargin)/2, 30);
        [self.contentView addSubview:_nameLabel];
     
        self.propertyLabel = [UILabel new];
        _propertyLabel.frame = CGRectMake(_nameLabel.frame.size.width+_nameLabel.frame.origin.x, _nameLabel.frame.origin.y, (Screen_Width-2*leftMargin)/2-30, 30);
        [self.contentView addSubview:_propertyLabel];
        
        self.assessoryView = [UIImageView new];
        _assessoryView.frame = CGRectMake(_propertyLabel.frame.size.width+_propertyLabel.frame.origin.x, _nameLabel.frame.origin.y+10, 7, 11);
        [self.contentView addSubview:_assessoryView];
    }
    return self;
}
Q3:UITableView滑动cell,界面卡顿的原因及其优化方案?

A3:
界面卡顿的原因是:cell赋值内容时,会根据内容设置布局,也就可以知道cell的高度,若有1000行,就会调用1000次 cellForRow方法,而我们对cell的处理操作,都是在这个方法中赋值,布局等等,开销很大。
1> cell上面如果有图片显示,那么下载图片一定要用缩率图,一定要进行异步下载,不然同步下载的话,随着cell的条数增加,界面就会会卡顿。
2> cell上面的图像和背景尽量使用不透明视图,因为半透明的视图需要APP去消耗性能进行渲染
3> cell界面上面的内容不宜太复杂,图片显示用小图,点击查看用大图
4> 不要重复创建不必要的cell,尽量重用cell
5> 文字图片异步加载,可以使用drawInRect绘制
6> 减少cell的刷新,尽量使用局部刷新
7> 下面两行代码据说可以增加流畅度
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

未完待续...

Q4:UITableView在一些特殊的界面。设置某行cell不可用,在didSelecte方法里面设置了不管用?

需要在cellForRow方法里面进行设置,才会起作用

Q5:iOS tableHeaderView的frame设置问题,cell遮挡tableHeaderView?

A5:headerView的frame必须在self.tableView.tableHeaderView = myView之前设置,否则设置无效。
tableView 滚动到顶端:
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

相关文章

网友评论

      本文标题:iOS-UITableViewCell的一些事

      本文链接:https://www.haomeiwen.com/subject/tjwyjqtx.html