Ios 在UITableview//Swift 4.2中自动布局多行标签
我编写这样的扩展是为了在UITableview中显示我的数据。但有时我的数据可能包含不止一行,我需要创建一些内容来显示完整的内容。如何更改我的代码(如下)来执行此操作Ios 在UITableview//Swift 4.2中自动布局多行标签,ios,swift,uitableview,swift4.2,Ios,Swift,Uitableview,Swift4.2,我编写这样的扩展是为了在UITableview中显示我的数据。但有时我的数据可能包含不止一行,我需要创建一些内容来显示完整的内容。如何更改我的代码(如下)来执行此操作 extension ViewController: UITableViewDataSource, UITableViewDelegate { // Define no of rows in your tableView func tableView(_ chatHistoryTable: UITableView,
extension ViewController: UITableViewDataSource, UITableViewDelegate {
// Define no of rows in your tableView
func tableView(_ chatHistoryTable: UITableView, numberOfRowsInSection section: Int) -> Int {
return messagesData.count
}
func tableView(_ chatHistoryTable: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = chatHistoryTable.dequeueReusableCell(withIdentifier: "userMessage")! as UITableViewCell
cell.textLabel!.text = messagesData[indexPath.row]
cell.textLabel!.textAlignment = .right
return cell;
}
}
我想我也应该为UITableViewCell写些东西,但我不知道,我说的对吗
请帮我回答这个问题。在这里,您需要遵循两个更改
- 最初设置的估计高度和auntomaticdimension,在页面上调用以下行
例如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 } }
- 第二个是单词包装,第二个是标签。在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; } }
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
步骤3:使标签支持多行
textLabel.LineBreakMode = UILineBreakMode.WordWrap;
textLabel.Lines = 0;
看看这个答案@vpoltave here标签不是通过代码创建的。。