【问题标题】:How to reduce the space between the table cells如何减少表格单元格之间的空间
【发布时间】:2019-02-15 20:03:30
【问题描述】:

单元格中的内容居中对齐。 这导致单元格被不需要的空白分隔。

我试过下面的代码

在 viewDidLoad 中

 tableView.layoutMargins = UIEdgeInsets.zero
 tableView.separatorInset = UIEdgeInsets.zero
 tableView.separatorStyle = .none

在 cellForRowAt 中

 cell.layoutMargins = UIEdgeInsets.zero
 cell.separatorInset = .zero

我正在寻找一种实用方法来自定义单元格之间的空间。

请在下面找到图片

Cell row height

【问题讨论】:

  • 张贴你的tableviewcell图片

标签: ios swift4.2


【解决方案1】:

单元格之间正好有0 空间,因此单元格本身必须有顶部/底部边距。

为了简单测试我刚才所说的,将单元格的背景设置为.red。如果我错了,你的红细胞之间会有白色条纹。

要做到正确:

  • 在单元格内查找顶部/底部边距/约束并将其移除
  • 确保所有约束都使行高明确,并使用automaticDimension 或自行计算行高并对其进行硬编码。

【讨论】:

    【解决方案2】:

    使用tableView:heightForRowAtIndexPath:。这使您可以设置大小,而不管单元格中的内容如何。

    【讨论】:

    • 这只有在我们有静态固定文本时才有用。但是,就我而言,单元格中的内容因行数而异
    【解决方案3】:

    根据您的单元配置,也许设置automaticDimension 可以解决它:

    self.tableView.rowHeight = UITableView.automaticDimension
    

    否则,您可能需要降低单元格高度:

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

    希望这会有所帮助。

    【讨论】: