Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法在tableview单元格中容纳多行_Ios_Swift_Uitableview - Fatal编程技术网

Ios 无法在tableview单元格中容纳多行

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

我试图在一个表视图单元格中有一个标题和多行,但是单元格没有扩展标题和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行
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
}