UITableViewCell 缩进仅适用于默认单元格 textLabel
UITableViewCell indentation works only for default cell textLabel
我有自定义 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 说 containerView
,并将单元格 contentView
的所有元素移动到 containerView
中。后来我做了IBOutlet
of "constainerView leading constraint"。
然后在 cellForRow: atIndexPath
table 视图委托中将其常量更新为:
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!
我有自定义 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 说 containerView
,并将单元格 contentView
的所有元素移动到 containerView
中。后来我做了IBOutlet
of "constainerView leading constraint"。
然后在 cellForRow: atIndexPath
table 视图委托中将其常量更新为:
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!