【问题标题】:How to set the rowHeight of the first cell in a tableView如何设置tableView中第一个单元格的rowHeight
【发布时间】:2019-11-03 19:21:17
【问题描述】:

我想更改表格视图中第一个(顶部 = 索引 0)单元格的高度,同时保持所有其他单元格的高度相同。

我该怎么做?

理想情况下,我希望能够在这里做到这一点:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   if shouldDisplaySuggestions && indexPath.row == 0 {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = "help"
        cell.textLabel?.font = UIFont(name: "SFCompactDisplay-Semibold", size: 17)

        return cell
    }

我将所有其他单元格设置为:

tableView.rowHeight = 64.07

而这并不存在 ->

tableView.cellForRow(at: IndexPath(row: 0, section: 0)).row...

【问题讨论】:

  • 这就是heightForRowAt 方法的用途。
  • 为什么行高为64.07?为什么不只是64
  • 我喜欢完美的设计,尽管我认为无论如何它都是默认的,在这种情况下我还不如对吧? @rmaddy

标签: ios swift uitableview uikit row-height


【解决方案1】:

你需要实现委托方法heightForRowAt

func tableView(_ tableView: UITableView, 
     heightForRowAt indexPath: IndexPath) -> CGFloat {
      return indexPath.row == 0 ? 200 : 100
}

如果您愿意,也可以为其他单元格返回动态

return indexPath.row == 0 ? 200 : UITableView.automaticDimension

提示而不是

let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")

始终使用

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)!

【讨论】:

  • 鉴于 OP 正在设置表格视图的rowHeight,最好使用return indexPath.row == 0 ? 200 : tableView.rowHeight
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 2020-08-17
相关资源
最近更新 更多