[英]Why is my uitableview cut off with too many cells ? (swift)
我有一個帶有uitableview的視圖控制器,以顯示一些注釋。 我使用情節提要和自動布局。
單元格的高度取決於內容。
當我有很多單元格時,我的表視圖將被切斷,並且無法完全顯示。 但是用較少的單元格是正確的。
在viewDidLoad中:
self.commentsTableView.estimatedRowHeight = 93.0
self.commentsTableView.rowHeight = UITableViewAutomaticDimension
在viewDidAppear中,我嘗試了兩種方法,首先使用高度限制:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.commentsTableView.removeConstraint(self.tableViewHeightConstraint)
self.tableViewHeightConstraint = NSLayoutConstraint(item: self.commentsTableView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: self.commentsTableView.contentSize.height)
self.commentsTableView.addConstraint(self.tableViewHeightConstraint)
self.commentsTableView.setNeedsUpdateConstraints()
self.commentsTableView.updateConstraintsIfNeeded()
}
我也嘗試了框架的高度(沒有高度限制):
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
var frame:CGRect = self.commentsTableView.frame
frame.size.height = commentsTableView.contentSize.height
self.commentsTableView.frame = frame
}
2種方法的結果完全相同。 當單元格過多時,表視圖不會完全顯示。
編輯
屏幕截圖。 藍色是我的滾動視圖。
我找到了解決方案。
在我的viewDidAppear中,首先根據其內容大小設置表格視圖的框架,然后根據表格視圖的高度設置滾動視圖的大小。
解決方案是做相反的事情,首先將scrollview設置為tableview的內容大小,然后使用scrollview的高度調整tableview。 而且有效!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.scrollView.contentSize=CGSizeMake(self.scrollView.frame.size.width, self.commentsTableView.frame.origin.y + commentsTableView.contentSize.height)
var frame:CGRect = self.commentsTableView.frame
frame.size.height = scrollView.contentSize.height
self.commentsTableView.frame = frame
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.