Make UITableView not scrollable and adjust height

2020-02-02 10:58发布

I'm embedding a UITableView inside of another UIScrollView. I only want the UIScrollView to scroll, not the UITableView, so I want to disable the scrolling in UITableView, as well as expand the contentHeight for the UITableView to accommodate all dynamic at once.

How would I go about doing this?

8条回答
叛逆
2楼-- · 2020-02-02 11:43

Basically, tableview's content size is all you need.

  1. Set tableview scrolling to false because we don't want scolling behaviour.

  2. Get the reference of tableview's height constraint.

  3. set tableViewHeightConstraint.constant = tableView.contentSize.height

Make sure, tableview's top, bottom, leading and trailing constraint and are in-place.

查看更多
【Aperson】
3楼-- · 2020-02-02 11:44

While this is very simple to accomplish with a fixed-content tableview as a subview in a scrollview, what is it you are exactly trying to accomplish? Dynamic cells with fixed content above and below? Permanently displayed content with a sliding section in the center?

Perhaps a cleaner design would be using a single UITableView with a Table Header and Footer, or a Section Header an Footer leveraging the automatic behavior provided by UITableViewStyleGrouped vs UITableViewStylePlain

That said, other answers seem to form a decent solution:

Set scroll enabled false on table view
Set tableview content size to sum of all cell sizes
Set tableview frame to content size
Adjust scroll view frame to account for dynamic table size if necessary
Add table view to scroll view amidst other content

Another option for manipulating scroll behavior would be checking the tableview or scrollview in the delegate scrollView shouldStartScrolling or similar methods, as this would allow more flexibility over the UI controls

查看更多
祖国的老花朵
4楼-- · 2020-02-02 11:49

You need to set the UITableView scroll to false,

tableview.scrollEnabled = false;

tableview.frame = CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y, tableview.frame.size.width, tableview.frame.size.height+(resultArray.count*HEIGHT_OF_CELL));

And then you can add the UITableView on UIScrollView by setting it's content size as UITableView height.

查看更多
放我归山
5楼-- · 2020-02-02 11:49
  1. Make your tableview frame cover all lines possible -> no scroll for table view
  2. Set contentSize of your scrollview = size of your tableViews.
查看更多
三岁会撩人
6楼-- · 2020-02-02 11:50

Set tableView size equal with contentSize.

override func viewDidLayoutSubviews() {
    tableView.frame.size = tableView.contentSize
}
查看更多
▲ chillily
7楼-- · 2020-02-02 11:50
  • First of all you need is to set up the UITableView Scrolling to NO
  • Second, create a height constraint for the table view and after you call [UITableView reloadData] change your height constraint

    tableViewHeightConstraint.constant = numberOfRows * heightOfEachRow;
    
查看更多
登录 后发表回答