【发布时间】:2016-12-11 18:35:58
【问题描述】:
我正在尝试在 tableView 中显示自定义视图,并让 iOS 使用 UITableViewAutomaticDimension 计算每一行的高度。不幸的是,我的牢房尺寸不合适(高度不好)。
我的自定义视图定义如下(这部分我没有使用AutoLayout,原因很多,我不想使用它):
class MyView : UIView {
var label: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
func setupView() {
label = UILabel()
label.numberOfLines = 0
addSubview(label)
}
func setText(text: String) {
label.text = text
setNeedsLayout()
}
override func layoutSubviews() {
super.layoutSubviews()
label.frame = bounds
invalidateIntrinsicContentSize()
}
override var intrinsicContentSize: CGSize {
guard let text = label.text else { return .zero }
let width = bounds.width
let height = text.heightWithConstrainedWidth(width: width, font: label.font)
return CGSize(width: width, height: height)
}
}
现在,我将这个自定义视图包装在 UITableViewCell 中,以便在 UITableView 中使用它。这里我使用的是 AutoLayout:
class MyCell : UITableViewCell {
var myView: MyView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupCell()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupCell()
}
func setupCell() {
myView = MyView()
contentView.addSubview(myView)
myView.snp.makeConstraints { make in
make.edges.equalTo(contentView)
}
}
}
现在,使用下面的 viewController,我显示一个带有大文本的单元格,看看它的高度是否被自动计算:
class ViewController: UIViewController {
var tableView: UITableView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableView = UITableView()
view.addSubview(tableView)
tableView.snp.makeConstraints { make in
make.edges.equalTo(view)
}
tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 30
tableView.dataSource = self
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell
cell.myView.setText(text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur")
return cell
}
}
不幸的是,单元格的高度等于 21。即使最后一次调用 MyView 返回的高度是 143,但这是 143。这是结果的屏幕截图:
我知道MyView 的intrinsictContentSize 使用它的框架并且它不应该使用它,但是我不知道如何设计一个在给定宽度的情况下具有灵活高度的 UIView 子类。考虑到我在 MyView 的 layoutSubviews() 中调用 invalidateIntrinsicContentSize(),我认为它会起作用。
如何更改此代码以从一开始就具有良好的单元格高度?
【问题讨论】:
-
你能详细说明你为什么选择不在这里使用自动布局吗?这正是它擅长的事情。
-
因为在我的真实用例中,我有其他东西而不是简单的 UILabel。我有一个由很多子视图组成的非常丰富的 UIView。如果我为所有这些都保留自动布局,我最终会在 tableViews 中遇到性能问题。
标签: ios uitableview autolayout