[英]self sizing cell with uitableviewdiffabledatasource
我有一個詳細的 ViewController,其單元格由自定義的 uitableviewdiffabledata 定義,如下所示:
{ (_, indexPath, item) -> UITableViewCell? in
let color = UIColor(named: "blue")!
if let _ = item as? TextFieldItem, let cell = tableView.dequeueReusableCell(withIdentifier: "textField", for: indexPath) as? TextFieldTableViewCell {
cell.textField.text = recipe.wrappedValue.name
cell.textField.placeholder = NSLocalizedString("name", comment: "")
cell.selectionStyle = .none
cell.textChanged = nameChanged
cell.backgroundColor = color
return cell
} else if let imageItem = item as? ImageItem, let imageCell = tableView.dequeueReusableCell(withIdentifier: "image", for: indexPath) as? ImageTableViewCell {
imageCell.setup(imageData: imageItem.imageData)
return imageCell
} else if let _ = item as? AmountItem, let amountCell = tableView.dequeueReusableCell(withIdentifier: "times", for: indexPath) as? AmountTableViewCell{
amountCell.setUp(with: recipe.wrappedValue.timesText, format: formatAmount)
amountCell.backgroundColor = color
return amountCell
} else if item is InfoItem {
return InfoTableViewCell(infoText: Binding(get: {
return recipe.wrappedValue.info
}, set: updateInfo), reuseIdentifier: "info")
} else if let stripItem = item as? InfoStripItem, let infoStripCell = tableView.dequeueReusableCell(withIdentifier: "infoStrip", for: indexPath) as? InfoStripTableViewCell {
infoStripCell.setUpCell(for: stripItem)
return infoStripCell
} else if let stepItem = item as? StepItem {
let stepCell = StepTableViewCell(style: .default, reuseIdentifier: "step")
stepCell.setUpCell(for: stepItem.step)
return stepCell
} else if let detailItem = item as? DetailItem, let cell = tableView.dequeueReusableCell(withIdentifier: "detail", for: indexPath) as? DetailTableViewCell {
let title = NSAttributedString(string: detailItem.text, attributes: [.foregroundColor : UIColor.label])
cell.textLabel?.attributedText = title
cell.accessoryType = .disclosureIndicator
cell.backgroundColor = color
return cell
}
return UITableViewCell()
}
我想制作這樣定義的 InfoTableViewCell:
class InfoTableViewCell: UITableViewCell {
@Binding private var infoText: String
private var textView = UITextView()
init(infoText: Binding<String>, reuseIdentifier: String?) {
self._infoText = infoText
super.init(style: .default, reuseIdentifier: reuseIdentifier)
setup()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setup() {
self.addSubview(textView)
textView.fillSuperview()
textView.text = infoText
textView.delegate = self
textView.backgroundColor = UIColor(named: "blue")!
textView.font = UIFont.preferredFont(forTextStyle: .body)
}
}
extension InfoTableViewCell: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
self.infoText = textView.text
}
}
我想讓這個單元格根據 textField 中的文本調整大小。 關於如何做到這一點的任何提示? PS 我正在使用 LBTA 工具用 textField 填充單元格的內容。
根據我的經驗,只需省略tableView(_:heightForRowAt:)
表視圖委托。 然后在創建單元格時,創建將單元格的頂部和底部綁定到內容的頂部和底部的約束。 然后細胞將增長或縮小以滿足約束條件。
在您的自定義表格視圖單元格 class 的init
中:
textView.topAnchor.constraint(equalTo: self.topAnchor, constant: 8).isActive = true
textView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -8).isActive = true
不確定 LBTA 工具將如何影響此行為,因此這可能僅適用於 vanilla UIKit。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.