我有自定义 UITableViewCell
,但无法在 cellForRow: atIndexPath:
中设置缩进。
cell.indentationWidth = 10.0
cell.indentationLevel = indexTuples.count //Dynamic
但是,如果 iOS 提供了默认单元格标签,则此方法有效:
cell.textLabel?.text = "TEST"
有什么限制吗?我该如何解决这个问题?
注意:我必须使用Autolayout
。
更新:
根据评论,我引用了 here ,我尝试了以下代码,但似乎没有帮助。
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.layoutMargins.left = CGFloat(self.indentationLevel) * self.indentationWidth
self.contentView.layoutIfNeeded()
}
最佳答案
In short : With the help of Autolayout leading constraint, I am able to solve this problem.
首先,我在单元格 contentView 中创建了一个 UIView say containerView
,并将单元格 contentView
的所有元素移动到 containerView
中。后来做了“constainerView leading constraint”的IBOutlet
。
然后在 cellForRow: atIndexPath
TableView 委托(delegate)中将其常量更新为:
cell.indentViewLeading.constant = 5 + CGFloat(10 * indexTuples.count - 1) //indexTuples.count gives dynamic values
cell.indentationWidth = 10.0
cell.indentationLevel = indexTuples.count
在我的 UITableViewCell 子类中:
@IBOutlet weak var indentViewLeading: NSLayoutConstraint!
关于ios - UITableViewCell 缩进仅适用于默认单元格 textLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47324155/