Ios 无法在tableview单元格中容纳多行
我试图在一个表视图单元格中有一个标题和多行,但是单元格没有扩展标题和3行以适应其中。Ios 无法在tableview单元格中容纳多行,ios,swift,uitableview,Ios,Swift,Uitableview,我试图在一个表视图单元格中有一个标题和多行,但是单元格没有扩展标题和3行以适应其中。 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("deviceCell", forIndexPath: in
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("deviceCell", forIndexPath: indexPath)
cell.textLabel?.text = "Title"
cell.detailTextLabel?.text = "Line1 " + "\n" + "Line2" + "\n" + "Line3"
return cell
}
这是我的代码,负责标题和3行
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("deviceCell", forIndexPath: indexPath)
cell.textLabel?.text = "Title"
cell.detailTextLabel?.text = "Line1 " + "\n" + "Line2" + "\n" + "Line3"
return cell
}
这就是我运行程序后的情况
您必须将单元格高度更改为自动沉淀 使用“自动布局”设置标签高度 在您的情况下:必须将标签高度设置为大于3行 文本 设置tableView.rowHeight=UITableViewAutomaticDimension tableView.estimatedRowHeight=您的估计高度
然后单元格将适合您的内容您必须将单元格高度更改为自动沉淀 使用“自动布局”设置标签高度 在您的情况下:必须将标签高度设置为大于3行 文本 设置tableView.rowHeight=UITableViewAutomaticDimension tableView.estimatedRowHeight=您的估计高度
然后单元格将适合您的内容添加这两种新方法它有助于解决此问题
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
添加这两种新方法有助于解决此问题
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}