最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]UITableViewCell中的contentView中的UILabel不显示

iOS crifan 3601浏览 0评论
[背景]
一个UITableViewCell中的contentView中的UILabel
在init函数中初始化属性,但是没有设置text内容:
            let timestampLabelTextColor = UIColor.whiteColor()
            let timestampBackgroundColor = UIColor(white: 0.734, alpha: 1.0)

            let timestampLabel = UILabel(frame: CGRectMake(0, LabelPadding, UIScreen.mainScreen().bounds.size.width, TimeStampLabelHeight))
            //timestampLabel.text = ""
            timestampLabel.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
            timestampLabel.textColor = timestampLabelTextColor
            timestampLabel.backgroundColor = timestampBackgroundColor
            timestampLabel.textAlignment = NSTextAlignment.Center
            timestampLabel.font = UIFont.boldSystemFontOfSize(TimeStampLabelFont)
            //timestampLabel.center.x = 350
            timestampLabel.numberOfLines = 1
            timestampLabel.sizeToFit()
            self.timestampLabel = timestampLabel

            print("timestampLabel.frame=\(timestampLabel.frame)")

            self.contentView.addSubview(timestampLabel)
            self.contentView.bringSubviewToFront(timestampLabel)
然后在后续的函数
configureCell
中去设置内容:
    override func layoutSubviews() {
        super.layoutSubviews()

        print("after layoutSubviews, timestampLabel.frame=\(timestampLabel?.frame)")
    }

configureCell{
            self.timestampLabel?.text = "昨天 22:08"
            timestampLabel?.setNeedsLayout()
            print("self.timestampLabel?.text=\(self.timestampLabel?.text)")
}
但是:
after layoutSubviews, timestampLabel.frame=Optional((0.0, 5.0, 55.0, 0.0))
并且,里面的label中text中的文字内容,也不显示。
[解决过程]
1.搜:

swift UILabel autoresizingMask
swift UILabel autoresizingMask not work
swift UITableViewCell contentView not show
参考:
才知道:
指的是:控件相对于父视图坐标值
而不是控件自己的内部的大小
后来是:
init时设置text的值
就可以了。。。

转载请注明:在路上 » [已解决]UITableViewCell中的contentView中的UILabel不显示

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.174 seconds, using 22.06MB memory