首页 > 解决方案 > PrototypeCell 上的空白区域

问题描述

UITableView有 2 个部分,第一个是显示 a SegmentControl,第二个是显示我的数据。第 2 部分还可以,但在 SegControl 部分,组件不适合正确的高度。

故事板

在此处输入图像描述

结果:空白区域出现在UISegmentedControl单元格下方。

在此处输入图像描述

我尝试了很多方法来解决这个问题:

   func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == 1 {
        return 100
    }
    return 44.0
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0.0
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return UIView()
}

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

    if section == 0 {
        return nil
    }
    .
    .
    }

编辑

我发现这个空白是页脚,但我不知道如何隐藏它。

在此处输入图像描述

        func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        view.backgroundColor = UIColor.red
        return view
   }

标签: iosswiftuitableview

解决方案


更新:添加额外空间的是节页脚视图,而不是单元格本身。

使用heightForFooterInSection. 以下代码应该能够解决您的问题。

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    if section == 0 {
      return 0
    }
    return UITableView.automaticDimension
}

推荐阅读