ios - 无法在表格 View 单元格中容纳多行

标签 ios swift uitableview

我试图在表格 View 单元格中包含一个标题和多行,但是该单元格没有扩展以适应“标题”和 3 行以适应那里。

这是我处理标题和 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
}

这是我运行程序后的样子

enter image description here

enter image description here

最佳答案

添加这两个新方法有助于解决这个问题

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

关于ios - 无法在表格 View 单元格中容纳多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890822/

相关文章:

ios - Stripe Basic PaymentIntent 确认付款错误

iphone - ASIFormDataRequest NSInvalidArgumentException 异常

iphone - ios label.text null

swift - UISearchController - 黑色矩形

swift - 如何在 Rx(Swift) 中以自然的方式组合序列和条件

Swift MQTT libmosquitto cleanSession 设置为 false 但无法设置持久订阅

ios - 如何防止搜索栏在滚动时消失? iOS swift

ios - 了解 bonjour 服务的状态

ios - 如何正确配置 UITableView 自定义单元格

ios - 在 iOS7 中使用 UICollectionView 的 UIRefreshControl