如何在tableView中设置第一个单元格的rowHeight

时间:2019-06-20 23:20:03

标签: ios swift uitableview uikit row-height

我想在表格视图中更改第一个(顶部=索引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...

1 个答案:

答案 0 :(得分:0)

您需要实现委托方法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)!