TableView dynamic height as scroll

I create a page that has a scroll view, and above it is a table view (scroll disabled). To do this, I gave the answers in this question - To make the UITableView not scrollable and adjust the height to accommodate all the cells , but failed.

View hierarchy along with constraints provided

- The main view

-Scroll view
attached to all sides of the main view (0,0,0,0), field restriction

- View content

attached to view the scroll (0,0,0,0), equal to the width of the main view, equal to the height of the main view (priority - 250)

-Table view inside content view

scrolling is disabled, having 50 points of space on all sides, Height (> =), lower distance 50 (ratio> =). I set more than equal to dynamically increase the height.

Now, when I populate the 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 } 

Therefore, when I run my code, it enlarges the view of the tableview, but does not stretch the size of the content, and it just becomes weird, since my scroll view does not scroll to the end of the table view, nor does my view of the table obey the auto layout constraint.

Please help with this. Thanks.

+5
source share
1 answer

I just needed to do this -

* delete the row - tableView.frame.size = tableView.contentSize

* Add height constraint for table view.

* Set priority to high

* Create an output height limit (Ctrl + Drag).

* Set a height limit on the height of the contents of the table.

 tablehtcnstrnt.constant = tableview.contentSize.height 

where you need to reload your table data.

Hope this helps other users.

+23
source

Source: https://habr.com/ru/post/1239108/


All Articles