【问题标题】:Tableview cell padding and spacingTableview 单元格填充和间距
【发布时间】:2020-03-04 06:58:00
【问题描述】:

我有这张表,其中包含有图像和文本的单元格

我想要的第一件事是向单元格添加填充,以便带有圆圈和剪刀的图像不会碰到单元格的边框。我已经尝试过cell.layoutMargins = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)cell.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)cell.separatorInset = UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100) 之类的方法,但没有任何改变。

我想要的第二件事是在单元格之间添加一个间距,我尝试了我找到的所有解决方案,但它似乎不起作用

CODE(我尝试过的一些解决方案都写在下面,但我也一次尝试过)

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "difficultyCell", for: indexPath)
        cell.textLabel?.text = myDict[indexPath.row].key 
        cell.textLabel?.font = .boldSystemFont(ofSize: 20)
        let imageCell = UIImage(named: "scissors.png")
        cell.imageView?.image = imageCell
        cell.backgroundColor = Esercizio.hexStringToUIColor(hex: "#4cf584").withAlphaComponent(0.85)
        cell.layer.borderColor = UIColor.orange.cgColor
        cell.layer.borderWidth = 1
        cell.layer.cornerRadius = 8
        cell.layoutMargins = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)
        cell.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
        cell.separatorInset = UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100)
        return cell
    }

【问题讨论】:

  • 你在使用自动布局吗?
  • @SGDev nope only CGRect
  • 如果你想要填充和间距使用UICollectionView
  • 您可以查看此链接以获取各种解决方案。 stackoverflow.com/questions/6216839/…

标签: ios swift uitableview padding spacing


【解决方案1】:

这里最好的解决方案是创建自己的单元格,在 contentView 中添加自己的图像、标签等,并使用自动布局约束调整它们。

见:https://www.raywenderlich.com/8549-self-sizing-table-view-cells

【讨论】:

    【解决方案2】:

    为了轻松解决,如果您使用自定义 UITableViewCell 文件,在您的单元格类中覆盖该类的 layoutsubView 方法,您可以将 UIEdgeInset 添加到 contentView 的框架中,并且不要忘记添加的填充量到表格视图中的总 RowHeight 估计:此代码肯定会为您工作,

    override func layoutSubviews() {
       super.layoutSubviews()
        contentView.frame = contentView.frame.inset(by:  UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0))
        }
    

    通过这种方式,您可以在要添加填充的任何方向创建填充。

    【讨论】:

      猜你喜欢
      • 2010-09-25
      • 1970-01-01
      • 2013-03-09
      • 2012-11-24
      • 2016-01-15
      • 2016-06-29
      • 2019-01-31
      • 1970-01-01
      • 2020-03-08
      相关资源
      最近更新 更多