灵心如玉,守一生无惧|

SadicZhou

园龄:3年2个月粉丝:7关注:4

IOS-开发获取tableview中cell的最终渲染宽度

如图

 我想要实现一个cell,里面有一个白色的消息区域宽度是整个cell的宽度减少20pt,

 于是我写了

1
_msgview.frame =CGRectMake(10, _time.bounds.origin.x+30, self.contentView.bounds.size.width-20, 80);

贴上完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier {
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
     
    self.selectionStyle = UITableViewCellSelectionStyleNone;//设置无点击样式
    self.backgroundColor =[UIColor colorWithRed:240/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
   
    _time =[[UILabel alloc] init];
    _time.textAlignment=NSTextAlignmentCenter;
    _time.text=@"";
    _time.font = [UIFont systemFontOfSize:11];
    _time.textColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1.0];
    _time.frame = CGRectMake(10, 5, self.contentView.bounds.size.width-20, 15);
    [self.contentView addSubview:_time];
     
    _msgview = [[UIView alloc] init];
    _msgview.frame =CGRectMake(10, _time.bounds.origin.x+30, self.contentView.bounds.size.width-20, 80);
    _msgview.backgroundColor=[UIColor whiteColor];
    _msgview.layer.cornerRadius = 8;
    [self.contentView addSubview:_msgview];
     
    _tit = [[UILabel alloc] init];
    _tit.frame = CGRectMake(10, 15, _msgview.frame.size.width-20, 15);
    _tit.text = @"";
    _tit.font =[UIFont systemFontOfSize:13];
    [_msgview addSubview:_tit];
     
    _msg = [[UILabel alloc] init];
   
    _msg.font=[UIFont systemFontOfSize:11];
    _msg.textColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1.0];
    _msg.numberOfLines = 0;
    [_msgview addSubview:_msg];
    return  self;
}

  

 

这样写的效果

 很明显此时的self.contentView.bounds.size.width 不是最终的cell的宽度,应该是初始化时候的宽度。

那么如何获取最终的cell宽度呢。

需要知道一个方法layoutSubviews。在iOS,UIViewlayoutSubviews方法会在以下情况被触发调用:

1. 初始化时

UIView初始化完成后,会立即调用layoutSubviews方法。

2. 布局发生改变时

如果改变了UIView的边界或子视图的排布,会调用layoutSubviews方法。例如添加/删除子视图,改变bounds,设置autoresizingMask等。

3. 调用setNeedsLayoutlayoutIfNeeded

调用view.setNeedsLayout()会标记需要重新布局,之后合适时机会调用layoutSubviews

调用view.layoutIfNeeded()会立即触发layoutSubviews

4. 旋转屏幕时

当界面旋转时,也会触发布局变化,调用layoutSubviews

5. 视图显示到窗口时

当一个视图将要显示到窗口上时,例如控制器的viewDidAppear,会调用layoutSubviews进行布局。

6. 滚动视图滚动结束时

例如UIScrollView滚动结束会调用layoutSubviews

可以知道当页面重新布局时会触发layoutSubviews方法,所以我们在自定义的cell类中重写它的layoutSubviews方法。

1
2
3
4
5
6
7
- (void)layoutSubviews {
    //如果写样式要用到self的长宽高需要在layoutSubviews方法里获取到self的最新宽高
  [super layoutSubviews];
 
    _msgview.frame =CGRectMake(10, _time.bounds.origin.x+30, self.contentView.bounds.size.width-20, 80);
 
}

在这个方法里面 self.contentView.bounds.size.width 可以获取到视图更新后的 cell宽度。此时用它来给_msgview设置frame即可。
最终实现了我们想要的效果

 

总结:如果自定义cell时,某个元素要用到cell本身的宽高来确定元素的frame时,需要在cell的layoutSubviews方法里获取到cell的最新宽高。

1
 
1
 
 

本文作者:ZihangChu

本文链接:https://www.cnblogs.com/SadicZhou/p/17681416.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   SadicZhou  阅读(140)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 尚好的青春 孙燕姿
  2. 2 孙燕姿
  3. 3 克卜勒 孙燕姿
- 孙燕姿
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.