ios – UILabel在启动时不在自定义UITableViewCell中执行多行,但在滚动后执行

编辑#1

我在github上添加了一个项目链接:
https://github.com/trestles/testtable

这是我第一次与Autolayout打交道,所以我希望我会做一些业余错误.老实说,我知道我是如何做这个操纵帧,但无法通过内容裁剪自动布局正常工作.部分问题是,如果我们总是处于纵向模式,我应该使用框架吗?

我有一个自定义的UITableViewCell,我有一些UILabel.它们设置为numberOfLines = 0.有时,他们会截断文本.像这样:

我该如何解决?我试过在viewDidLoad中重新加载数据,但这似乎并不重要.大多数情况下,当您滚动时,它会自行修复(但并非总是如此).它可以是任何三个UILabel,并且与文本数量无关.我第一次使用带有自动布局的UILabels,所以很可能是我犯了一些错误.这是我的UILabel属性:

和第一个标签的布局:

解决方法

您的自动布局非常完美,只是因为您在xib中使用默认文本设置自动布局而出现问题.并在您viewDidLoad中,您正在更新UILabel文本但不以编程方式更新布局.所以只留下一行如下.
self.mainTV.layoutIfNeeded();

在viewDidLoad方法中重新加载UITableView之前添加上面的行.所有的事情都很好.

示例:

override func viewDidLoad() {
    super.viewDidLoad()

    self.mainTV.dataSource=self
    self.mainTV.delegate=self
    self.mainTV.rowHeight = UITableViewAutomaticDimension
    self.mainTV.estimatedRowHeight = 84.0
    //self.mainTV.registerClass(MyTableViewCell.self,forCellReuseIdentifier: "MyCustomCell")


    var tmps = [String]()

    tmps.append("ABC But here is an artist. He desires to paint you the dreamiest,shadiest,quietest,most enchanting bit of romantic landscape in all the valley of the Saco. What is the chief element he employs?")
    tmps.append("DEF But here is an artist.")
    tmps.append("GHI But here is an artist. He desires to paint you the dreamiest,most enchanting bit")

    for var i=0; i<10; i++
    {
      var menuItem=EKMenuItem()
      let randomIndex1 = Int(arc4random_uniform(UInt32(tmps.count)))
      menuItem.header = "\(i) \(tmps[randomIndex1])"

      let randomIndex2 = Int(arc4random_uniform(UInt32(tmps.count)))
      menuItem.detail = "\(i) \(tmps[randomIndex2])"
      let randomIndex3 = Int(arc4random_uniform(UInt32(tmps.count)))
      menuItem.price = "\(i) \(tmps[randomIndex3])"
      //tmpItem.price = "my price"
      dataItems.append(menuItem)
    }

    self.view.backgroundColor = UIColor.greenColor()

    self.mainTV.layoutIfNeeded();
    self.mainTV.reloadData()
    // Do any additional setup after loading the view,typically from a nib.
  }

希望这对你有所帮助.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...