UILabel 在启动时不会在自定义 UITableViewCell 中执行多行操作,但会在回滚后执行

UILabel doesn't do multiline in a custom UITableViewCell on launch but does after scrolled-back

编辑#1

我在此处 github 上的项目中添加了一个 link: https://github.com/trestles/testtable

这真的是我第一次处理 Autolayout,所以我预计我会犯一些业余错误。老实说,我知道我将如何操作框架,但无法通过内容剪辑来正确使用自动布局。部分问题是,如果我们始终处于纵向模式,我是否应该只使用相框?


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

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

第一个标签的布局:

更新单元格约束和布局

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomCell") as! MyTableViewCell
    cell.headerLabel.text=dataItems[indexPath.row].header
    cell.setHeader(dataItems[indexPath.row].header)
    cell.setDetail(dataItems[indexPath.row].detail)
    cell.setPrice(dataItems[indexPath.row].price)
    // update constraint and layout
    cell.layoutIfNeeded()
    return cell
  }

只需在viewDidLayoutSubviews()中添加self.mainTV.reloadData()即可解决您的问题。

您可以在此处查看更多详细信息。

http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html

我已经从 git 下载了源代码。我不确定这是否是错误。但是在 viewdidappear 中重新加载 table 解决了这个问题。

override func viewDidAppear(animated: Bool) {

        mainTV.reloadData()

    }

这是执行上述操作时在模拟器上的显示方式。

您的 自动布局 完美 ,只是因为您将 auto layout 设置为 默认值才出现问题text 在你的 xib 中。在您 viewDidLoad 中,您正在更新 UILabel 文本但不是以编程方式更新 layout。所以只剩下一行如下。

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, shadiest, quietest, 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.
  }

希望对您有所帮助。

我看到您只在高度上设置了约束,没有在宽度上设置约束。 也尝试设置它。