【问题标题】:Table View Cell Row Height doesnt work表格视图单元格行高不起作用
【发布时间】:2018-03-13 04:10:00
【问题描述】:

我尝试硬编码表格视图单元格中的行高。运行程序后,它看起来只有一行。我怀疑这是因为表格视图的行高。

你能告诉我这里出了什么问题吗?

【问题讨论】:

  • 高度似乎没有问题,您是否还从代码中的委托方法设置了高度?如果是这样,请删除该方法。
  • 您的标签行号是多少?它应该是 0,你的约束集呢?另一方面,你会分享你是否为动态约束表视图高度设置?
  • 你可以查看我的回答希望这对你有帮助stackoverflow.com/a/36574334/1776470

标签: ios swift uitableview row-height


【解决方案1】:

在你的 UITableViewDelegate 中添加这段代码

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

【讨论】:

  • 不,不要这样做。如果所有行的高度相同,最好在viewDidLoad 中简单地设置表格视图的rowHeight 属性一次。只有在不同的行有不同的高度时才实现这个委托方法。
  • @rmaddy 根据苹果文档,委托的性能优于属性。
  • @dahiya_boy 您误读了文档。 UITableView rowHeightUITableViewDelegate heightForRowAt 的文档明确指出委托方法是一个性能问题。设置rowHeight 属性比使用委托方法(假设所有行具有相同的高度)要高效得多。
【解决方案2】:

Xcode 9 中,tableview 会根据Autolayout 自动创建估计单元格。由于您没有提供自动布局,这就是您得到这个的原因。

您需要通过选择 tableview 并取消选中两个选项来禁用估计和自动单元格大小:

给你想要的行高值,将相应地创建单元格。

希望这能有所帮助。

【讨论】:

  • 如果我有不同类型的不同高度的原型单元,我该怎么办?
【解决方案3】:

如果希望单元格根据其内容占用空间,则需要在表格视图委托方法heightForRowAt中返回UITableViewAutomaticDimension:和estimatedHeightForRowAt强>:

【讨论】:

  • 你也可以使用:yourTable.rowHeight = UITableViewAutomaticDimension yourTable.estimatedRowHeight = UITableViewAutomaticDimension
【解决方案4】:

确保按照以下步骤制作,自动尺寸对单元格/行高布局有效。 (这里是 UILabel 的示例代码以及如何为表格单元格设置内容特定高度。)

  • 分配和实现 dataSource 和委托
  • UITableViewAutomaticDimension 分配给rowHeight 和estimatedRowHeight
  • 实现委托/数据源方法(即heightForRowAt并返回一个值UITableViewAutomaticDimension给它)

-

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Don't forget to set dataSource and delegate for table
    table.dataSource = self
    table.delegate = self

    // Set automatic dimensions for row height
    // Swift 4.2 onwards
    table.rowHeight = UITableView.automaticDimension
    table.estimatedRowHeight = UITableView.automaticDimension


    // Swift 4.1 and below
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension

}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    // Swift 4.2 onwards
    return UITableView.automaticDimension

    // Swift 4.1 and below
    return UITableViewAutomaticDimension
}

对于 UITableviewCell 中的标签实例

  • 设置行数=0(&换行模式=截尾)
  • 设置与其父视图/单元格容器相关的所有约束(上、下、左右)。
  • 可选:设置标签的最小高度,如果你想要标签覆盖的最小垂直区域,即使没有数据。

【讨论】:

    【解决方案5】:

    viewDidLoad() 中将行高和估计行高设置为自动

    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension
    

    heightForRowAt: 中设置高度为UITableViewAutomaticDimension

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    

    注意:标签long restaurant name的行数设置为0。

    【讨论】:

      【解决方案6】:

      按照对我有用的this guide,您必须添加一堆约束,然后自动计算行高。

      简而言之,我做了什么:(添加和修改约束是故事板视图右下角的两个最右边的按钮)我首先选择了原型单元格中的所有视图,删除了所有约束(最右边有一个不错的选项按钮),然后添加所有缺少的约束(那里的另一个不错的选项),最后我选择了右侧的第二个按钮并单击了所有红线。您可以在同时选择所有视图的同时执行此操作。然后就成功了。

      【讨论】:

        【解决方案7】:

        您可以在 tableviewdelegate 中手动完成:

        override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 100
        }
        

        【讨论】:

          【解决方案8】:
          1. 将行数设置为 0
          2. 设置
             yourTableView.rowHeight = UITableView.automaticDimension and 
             yourTableView.estimatedRowHeight =  UITableView.automaticDimension
          
          1. tableView(_:heightForRowAt:)委托方法中返回UITableView.automaticDimension

          2. 设置所有约束。底部约束对于增加表格视图单元格高度很重要。

          一切准备就绪

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多