在UITableview中自动布局多行标签// Swift 4.2

时间:2019-02-15 10:15:51

标签: ios swift uitableview swift4.2

我写了这样的扩展名以在UITableview中显示我的数据。但是有时我的数据可能包含多于1行,因此我需要创建一些内容以显示全部内容。我该如何更改下面的代码来做到这一点?

pip3 install selenium

我认为我也应该为UITableViewCell写一些东西,但是我不知道,我是正确的。

请帮助我解决这个问题。

2 个答案:

答案 0 :(得分:0)

在这里,您需要进行两项更改。

  • 最初设置Self Sizing Cell的估算高度和不规则尺寸,在页面加载时调用以下行。

      tableView.estimatedRowHeight = 44.0
      tableView.rowHeight = UITableView.automaticDimension
    

    例如

     @IBOutlet weak var yourTableviewName: UITableView!{
    didSet{
        yourTableviewName.tableFooterView = UIView()
        yourTableviewName.estimatedRowHeight = 44.0
       yourTableviewName.rowHeight = UITableView.automaticDimension
        }
      }
    
  • 第二个为您的自动换行,而numberOfLines为您的标签。按照cellforRow方法的以下行

       func tableView(_ chatHistoryTable: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = chatHistoryTable.dequeueReusableCell(withIdentifier: "userMessage")! as UITableViewCell
         cell.textLabel!.numberOfLines = 0
        cell.textLabel!.lineBreakMode = .byWordWrapping
        cell.textLabel!.text = messagesData[indexPath.row]
        cell.textLabel!.textAlignment = .right
    
        return cell;
    }
    
    }
    

答案 1 :(得分:0)

步骤1。在“自定义表格视图”单元格中添加完整的垂直约束链

第2步。设置单元格估算高度

tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension

第3步:使标签支持多行

textLabel.LineBreakMode = UILineBreakMode.WordWrap;
textLabel.Lines = 0;