【发布时间】:2018-10-08 18:11:35
【问题描述】:
资源:
我已经阅读了来自Using Auto Layout in UITableView for dynamic cell layouts & variable row heights 的多个答案
并遵循他们的建议,但它不起作用。
设置重现:
如果您复制/粘贴 MyTableViewCell 和 ViewController sn-ps: 那么您可以重现该问题。
我已将 MyTableViewCell 子类化并添加了我自己的标签。
import UIKit
class MyTableViewCell: UITableViewCell {
lazy var customLabel : UILabel = {
let lbl = UILabel()
lbl.translatesAutoresizingMaskIntoConstraints = false
lbl.numberOfLines = 0
return lbl
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupLayout()
}
private func setupLayout(){
contentView.addSubview(customLabel)
let top = customLabel.topAnchor.constraint(equalTo: contentView.topAnchor)
let bottom = customLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
let leadingFromImage = customLabel.leadingAnchor.constraint(equalTo: imageView!.trailingAnchor, constant: 5)
let trailing = customLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
NSLayoutConstraint.activate([top, bottom, leadingFromImage, trailing])
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
}
以下ViewController 包含我的表格视图:
import UIKit
class ViewController: UIViewController {
var datasource = ["It would have been a great day had Manchester United Lost its \n game. Anyhow I hope tomorrow Arsenal will win the game"]
lazy var tableView : UITableView = {
let table = UITableView()
table.delegate = self
table.dataSource = self
table.translatesAutoresizingMaskIntoConstraints = false
table.estimatedRowHeight = 100
table.rowHeight = UITableViewAutomaticDimension
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.pinToAllEdges(of: view)
tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "id")
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datasource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath) as! MyTableViewCell
cell.customLabel.text = datasource[indexPath.row]
logInfo(of: cell)
cell.accessoryType = .detailDisclosureButton
cell.imageView?.image = UIImage(named: "honey")
cell.layoutSubviews()
cell.customLabel.preferredMaxLayoutWidth = tableView.bounds.width
logInfo(of: cell)
print("---------")
return cell
}
private func logInfo(of cell: MyTableViewCell){
print("boundsWidth: \(cell.contentView.bounds.width) | maxLayoutWidth: \(cell.contentView.bounds.width - 44 - 15 - 5) | systemLayoutSizeFitting : \(cell.customLabel.systemLayoutSizeFitting(UILayoutFittingCompressedSize))")
}
}
extension UIView{
func pinToAllEdges(of view: UIView){
let leading = leadingAnchor.constraint(equalTo: view.leadingAnchor)
let top = topAnchor.constraint(equalTo: view.topAnchor)
let trailing = trailingAnchor.constraint(equalTo: view.trailingAnchor)
let bottom = bottomAnchor.constraint(equalTo: view.bottomAnchor)
NSLayoutConstraint.activate([leading, top, trailing, bottom])
}
}
我使用了honey image 的链接。我已将其大小设置为44 * 44
主要问题
我的主要问题在cellForRowAtIndex内部:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath) as! MyTableViewCell
cell.customLabel.text = datasource[indexPath.row]
logInfo(of: cell)
cell.accessoryType = .detailDisclosureButton
cell.imageView?.image = UIImage(named: "honey")
cell.layoutSubviews()
cell.customLabel.preferredMaxLayoutWidth = cell.contentView.bounds.width
logInfo(of: cell)
print("---------")
return cell
}
问题:
无论出于何种原因,赋值给:
cell.customLabel.preferredMaxLayoutWidth
似乎不对。
Q1:为什么会这样?
Q2:我在调用 cell.layoutSubviews 之前和之后记录 contentView 的绑定,它从 320 切换到 260 但最终在 viewDebugger 中显示为 @987654338 @!!!
为什么 contenView 的边界又变了?!
我已经从问题中删除了一些其他屏幕截图。它们大多杂乱无章,但也许值得一看。您可以查看修订历史记录。
【问题讨论】:
-
也许覆盖
MyTableViewCell中的布局方法,如layoutSubviews并在preferredMaxLayoutWidth中设置preferredMaxLayoutWidth而不是cellForRowAt:会起作用 -
我的代码当前序列不应该有同样的效果吗?
-
忽略边界变化,你想完成什么?很有可能您一开始就不需要该值...
-
@DonMag 这是问题初始版本中的image1。如果我没有在
customLabel上设置任何宽度约束,就会发生这种情况,它的侧面只有 4 个约束。使用preferredMaxLayoutWidth我正在尝试为其设置宽度。因此标签的高度将驱动该单元格所需的最大高度。我将cell.contentView.bounds.width分配给标签的preferredMaxLayoutWidth。但这并不准确。所以我在拨打layoutSubviews后分配了preferredMaxLayoutWidth。 UI 看起来更好,但并不完美
标签: ios swift uitableview autolayout contentsize