![](/img/trans.png)
[英]Self-sizing UITableViewCell with UITableView inside it - self-sizing doesn't work
[英]Self-sizing UITableViewCell without Auto Layout
我正在嘗試創建一個自動調整大小的UITableView
但不想使用自動布局來布局它的子視圖。
有沒有辦法做到這一點? 我正在設置
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 40
但是在layoutSubviews
中調整self.frame.size.height
沒有幫助,單元格保持在 40 高度,所以我假設我需要以不同的方法返回真實高度。 對於UICollectionViewCells
方法是
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes
但我找不到UITableViewCell
的等效項。
感謝您的指點。
您不能在沒有 auto-layout 的情況下創建動態高度 tableViewCell ,實際上當您將此UITableViewAutomaticDimension
傳遞給 tableView 的 rowHeight 時,它要么
1- 如果實現,則返回heightForCellAtRow
指定的高度
2- 從 Xib 返回正確的高度,或者如果您在代碼中創建了約束
3- 如果無效,它將返回 40(默認)
實現自調整大小的 UITableViewCell 沒有自動布局,你必須自己計算大小並在 sizeForRowAtIndexPath 中返回它:
class Cell: UITableViewCell {
...
func getCellHeight() -> CGFloat {
// calculate height yourself
}
...
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
guard let cell = tableView.cellForRow(at: indexPath) as? Cell else { return defaultHeight }
return cell.getCellHeight
}
或者您可以覆蓋sizeToFit()
、 sizeThatFits(size: CGSize)
來計算您的預期高度並在需要時調用這些方法。
它實際上是可行的,並且這是一種非常好的方法,無需自動布局(如果您想在TableView上平滑滾動)。
您可以通過使用sizeThatFits(_ size: CGRect)
方法通過考慮單元格內的所有視圖/填充來計算單元格的大小來實現這一點。 然后你將不得不像你自己一樣使用 autoDimension rowHight 讓 TableView 知道這一點,但也在func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
方法中:
UITableView 設置:
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = UITableView.automaticDimension
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
請記住,如果您的單元格中有帶有動態文本的標簽,則需要在sizeThatFits(_ size: CGRect)
方法中設置文本並從那里計算標簽高度。
UITableViewCell:
let cellLabel = UILabel()
var labelText: String?
private let padding: CGFloat = 8
override func sizeThatFits(_ size: CGSize) -> CGSize {
guard let text = self.labelText else { return .zero } // You might not need this
let superHeight = super.sizeThatFits(size).height
let verticalPadding = padding * 2
descriptionLabel.text = text
descriptionLabel.font = UIFont.rpx.regular14
let labelHeight = labelHeight.text?.boundingRect(
with: CGSize(width: size.width, height: size.height),
options: .usesLineFragmentOrigin,
context: nil
).size.height ?? size.height
return CGSize(width: size.width, height: superHeight + labelHeight + verticalPadding)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
let maxLabelWidth:CGFloat = tableView.frame.width
let title = UILabel()
label.numberOfLines = 0
let titleFont = [NSFontAttributeName: UIFont(name: "OpenSans", size: 10.0)! ]
let titleStr = "test Str" // update with your title string from Array
label.attributedText = NSMutableAttributedString(string: titleStr , attributes: titleFont)
let updatedSize:CGSize = title.sizeThatFits(CGSizeMake(maxLabelWidth, CGFloat.max))
let updatedHeight = updatedSize.height
return updatedHeight
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.