繁体   English   中英

单元格高度不增加取决于表格视图 swift 中的 label 文本

[英]Cell height is not increasing depend on the label text in tableview swift

我需要更改单元格高度取决于消息 label 文本

Cell.Xib 设计 img

对于消息 label 我已经给出了限制

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

和行数 = 0

现在 label 正在增加但单元格没有增加,为什么?

o/p

o/p 图像

代码:

 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 那么单元格不会增加,为什么? 请帮忙

您可以通过两个步骤简单地完成 go:

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.

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

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

在此处输入图像描述

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

label.numberOfLines = 0

第 3 步:在 ViewDidLoad Function

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM