【问题标题】:Swift set margins to the left and right side at TableViewCellsSwift 在 TableViewCells 的左侧和右侧设置边距
【发布时间】:2019-09-24 16:56:34
【问题描述】:

我想要一个这样的 TableView:

左右两侧有边距,顶部和底部有角边框。 没有什么对我有用。 我想在 TableViewController 中设置它。 我的单元格定义代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! TableViewCell

    cell.backgroundColor = colors[indexPath.row]

    cell.label.text = categories[indexPath.row]

    cell.selectionStyle = .none

    return cell
}

我的 TableView 现在:

有人可以帮帮我吗?

【问题讨论】:

    标签: swift uitableview margins


    【解决方案1】:

    不确定这是否是您要查找的内容。如果我错了,请纠正我,我相信它使用的 TableView 外观是 Grouped 样式。对不起,如果我完全不明白你的问题。

    【讨论】:

      【解决方案2】:

      要实现与图片对应的tableView,可以这样做:

      而不是将左右边距设置为tableViewCell,而是将其设置在tableView上。(以编程方式或在界面构建器中进行)

      tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
      tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
      

      并设置圆角

      // Change cornerRadius according your needs
      tableView.layer.cornerRadius = 10
      tableView.clipsToBounds = true
      

      【讨论】:

        猜你喜欢
        • 2018-08-29
        • 2018-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-04
        • 2021-09-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多