ios - Tableview 单元格内边距和间距

标签 ios swift uitableview padding spacing

我有一张表格,其中的单元格包含图像和文本 enter image description here

我想要的第一件事是向单元格添加填充,以便带有圆圈和剪刀的图像不会碰到单元格的边框。我已经尝试过诸如 cell.layoutMargins = UIEdgeInsets(top: 50, left: 50, Bottom: 50, right: 50)cell.bounds = CGRect(x: 0, y: 0,宽度:100,高度:100)cell.separatorInset = UIEdgeInsets(顶部:100,左侧:100,底部:100,右侧:100)但没有任何改变。

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

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

    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
    }

最佳答案

为了轻松解决这个问题,如果您使用自定义 UITableViewCell 文件,请在单元类中重写该类的layoutsubView方法,然后您可以将 UIEdgeInset 添加到 contentView 的框架中,并且不要忘记添加的填充量表格 View 中的总 RowHeight 估计:此代码肯定适合您,

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

这样您就可以按照您想要添加填充的任何方向创建填充。

关于ios - Tableview 单元格内边距和间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58753073/

相关文章:

ios - Swift:为什么它不正确地更新高分?

ios - 在 Swift 3 中编写 UnsafeMutablePointer

ios - 在 UITableViewController 上编辑 UITextField 时滚动区域不正确

ios - NSKeyedArchiver 和 NSKeyedUnarchiver

ios - UITableView 行移动怪异

ios - 为标识符 (CELLNAME) 注册的 nib 无效 - nib 必须恰好包含一个顶级对象,该对象必须是 UITableViewCell 实例

ios - 如何在 iWatch 中显示通知?

swift - 自动调整 UITableViewCell 内部 UITableView 的大小

ios - 如何在 iOS 11 中隐藏章节标题?

ios - 在代码中使两个 AutoLayout 约束相等?