我想要的第一件事是向单元格添加填充,以便带有圆圈和剪刀的图像不会碰到单元格的边框。我已经尝试过诸如 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/