簡體   English   中英

快速調整自定義單元的大小

[英]Trouble sizing custom cells in swift

因此,我使用Parse后端構建了一個簡單的Messenger。 tableView ,我顯示用戶之間的對話。 tableView的單元格根據標簽上的文本量調整大小。

我在viewDidLoad()使用此代碼來實現調整大小:

tableView.estimatedRowHeight = 51
tableView.rowHeight = UITableViewAutomaticDimension
tableView.setNeedsLayout()
tableView.layoutIfNeeded()

我使用的估計高度為51,因為這是我在情節提要板上的單元格的高度。 無論如何,我面臨的問題是,有時單元格的尺寸不正確,有時對於容納的單元格來說太大了。 我不知道為什么。 這是單元格的錯誤渲染 。

這是我通過簡單地上下滾動獲得的單元格的稍微正確的渲染。

。

這里發生了什么?

remove this lines from viewDidLoad

1)tableView.estimatedRowHeight = 51
2)tableView.rowHeight = UITableViewAutomaticDimension

use below two methods of tableview

override func tableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
return UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{
return UITableViewAutomaticDimension
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM