【问题标题】:Cell height is not increasing depend on the label text in tableview swift单元格高度不会增加取决于 tableview swift 中的标签文本
【发布时间】:2023-04-06 01:32:01
【问题描述】:

我需要根据消息标签文本更改单元格高度

Cell.Xib Design img

对于消息标签,我已经给出了约束

 trailing = leading = top = 10, height => 30 (greater then or equal to height)

和行数 = 0

现在标签在增加,但单元格没有增加,为什么?

o/p

o/p img

代码:

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return msgArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "MessageDetailsTableViewCell", for: indexPath) as! MessageDetailsTableViewCell
    self.tableView.separatorStyle = .none

    cell.msgLabel.text = msgArray[indexPath.row]
    cell.nameLabel.text = nameArray[indexPath.row]
    cell.timeLabel.text = timeArray[indexPath.row]
    cell.picImageview.image = UIImage(named: "icon12")
    
    return cell
    
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100//UITableView.automaticDimension
}

如果我给heightForRowAt = UITableView.automaticDimension 那么什么都没有,如果我将其高度固定为 100 则单元格不会增加,为什么?请帮忙

【问题讨论】:

  • 请看这个appcoda.com/self-sizing-cells,希望对你有帮助。
  • 检查你的单元格 lblheight 不会被修复。
  • 您需要修复标签的底部布局约束

标签: ios swift uitableview label tableview


【解决方案1】:

您可以通过两个步骤变得简单:

1. heightForRowAt 方法:

   func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
      return UITableView.automaticDimension
   }

2。 UILabel 属性

   yourLabel.numberOfLines = 0 // or you can set numberOfLines from storyBoard.

【讨论】:

    【解决方案2】:

    为此,您需要按照以下步骤操作。

    第 1 步: 将标签的约束设置为前导、尾随、顶部和底部。将底部约束设置为大于等于。

    第 2 步: 将标签的行数设置为 0。

    label.numberOfLines = 0
    

    第 3 步:在 ViewDidLoad 函数中

        self.tableView.rowHeight = UITableView.automaticDimension
        self.tableView.estimatedRowHeight = 150
    

    【讨论】:

      猜你喜欢
      • 2014-07-17
      • 1970-01-01
      • 2020-07-06
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多