ios - UITableViewCell 缩进仅适用于默认单元格 textLabel

标签 ios swift uitableview indentation

我有自定义 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/

相关文章:

ios - TRON 快速响应头

ios - 如何在两个 View 之间传递按钮?

objective-c - 这是什么意思? "Writable atomic property ' fileManager' 无法将合成的 setter/getter 与用户定义的 setter/getter 配对”

swift - collectionView.dequeueReusableCell 不调用 Cell 的 init

ios - UITableView insertRows 不锁定主线程

ios8-Facebook sdk-如何使用 UIWebView 打开应用程序请求 url

uiview - 可能是 Swift 编译器错误?

ios - 在 PHPhotoLibrary 中禁用删除请求确认

ios - 如何根据月份制作 UITableView 部分 - iOS

ios - 制作可搜索的 UITableView,当选择一个单元格时,它会将其带到不同的 ViewController,但单元格索引保持不变