問題描述
我正在設計一個具有滾動視圖的頁面,其上方是一個表格視圖(禁用滾動).為此,我在這個問題中提到了答案 - 使 UITableView 不可滾動并調整高度以適應所有單元格,但沒有成功.
I am designing a page having a scroll view and above it a table view(scroll disabled). For doing this I have referred answers in this question - Make UITableView not scrollable and adjust height to accommodate all cells ,but wasn't successful.
視圖的層次結構以及提供的約束-
Hierarchy of views along with provided constraints-
-主視圖
-滾動視圖
固定到主視圖的所有側面(0,0,0,0),限制到邊距
-Scroll view
pinned to all sides of main view(0,0,0,0), constraint to margins
-內容視圖
固定到滾動視圖(0,0,0,0),與主視圖等寬,與主視圖等高(優先級 - 250)
pinned to scroll view(0,0,0,0),equal width to main view,equal height to main view(priority - 250)
-內容視圖內的表格視圖
滾動禁用,從四面八方有50個點空間,高度(> =),底部間距50(關系> =).我已經把大于等于,以便動態增加高度.
scroll disabled,having 50 point spaces from all sides,Height(>=),bottom spacing 50(relation >=).I have put greater than equal so as to increase height dynamically.
現在,當我填充表格視圖時,我將代碼用作
Now when I populate my table view I use the code as
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableview.dequeueReusableCellWithIdentifier("cellreuse", forIndexPath: indexPath)
cell.textLabel?.text = name[indexPath.row]
tableview.frame.size = tableview.contentSize
return cell
}
因此,當我運行我的代碼時,它會增加 tableview 框架但不會拉伸內容大小,而且它會變得很奇怪,因為我的滾動視圖不會滾動到 table view 的末尾,我的 table view 也不服從自動布局約束.
So when I run my code, it increases the tableview frame but doesn't stretch the content size and it just becomes weird as my scroll view doesn't scroll to the end of the table view neither my table view obeys the auto layout constraints.
推薦答案
只是我需要這樣做 -
Just I needed to do this -
去掉這一行 -
tableView.frame.size = tableView.contentSize
為表格視圖添加高度約束.
Add a height constraint for table view.
將優先級設置為高
創建一個高度約束的出口(Ctrl+Drag).
Create an outlet of the height constraint(Ctrl+Drag).
無論您需要在何處重新加載表格數據,請將高度約束設置為表格視圖的內容高度.
Wherever you need to reload data of your table, set the height constraint to tableview's content height.
tableHeightConstraint.constant = tableview.contentSize.height
這篇關于滾動視圖中的動態高度 TableView的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!