ios - tableView 没有设置自动行高

标签 ios swift tableview row-height

在我的项目中,我有一个包含 3 个部分的静态 tableView。第二部分中的单元格包含一个动态填充的标签,因此具有动态高度。单元格应将其高度调整为标签的高度。这是我尝试过的,但没有成功:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.section == 0 {
      return 44
    } else if indexPath.section == 1 {
      return UITableViewAutomaticDimension
    } else if indexPath.section == 2 {
      return 80
    } else {
      return 50
    }
}

除自动尺寸外,所有部分的高度都已正确设置。有帮助吗?

最佳答案

viewDidLoad() 中设置这一行

tableView.rowHeight = UITableViewAutomaticDimension

然后写这个table view方法

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

还要确保您已正确使用自动布局。 并且您已经设置了 label = 0 的行数

关于ios - tableView 没有设置自动行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38356782/

相关文章:

ios - Swift JSON 进入TableViewCell

ios - 我怎样才能在 ios swift 中有问题地回拨到以前的应用程序?

ios - 更改 cellForRowAtIndexPath 中的 NSLayoutConstraint 常量不断导致 "unable to satisfy constraints"错误

ios - 更改字符串中特定单词的颜色

ios - 如何停止在横向模式下使用单元格向上滚动表格 View 标题?

iphone - 如何覆盖@synthesized getter?

swift - 在屏幕的某些部分禁用用户交互

ios - 如何在捕获图像时在相机屏幕中添加任何自定义图像?

javafx - 如何在 TableView 中复制/粘贴表格单元格

ios - 如何使 UItableview 中的特定静态单元格根据其内部内容的约束进行 self 调整