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:50

UITableView is a scroll view. While you are not supposed to nest UIScrollviews, you can disable scrolling of the the table view.

查看更多
相关推荐>>
3楼-- · 2020-02-02 11:55

Set tableView scrolling property to false.

First Approach:

  1. Create a subclass for tableView and override intrinsicContentSize.

         class MyOwnTableView: UITableView {
        override var intrinsicContentSize: CGSize {
            self.layoutIfNeeded()
            return self.contentSize
        }
    
        override var contentSize: CGSize {
            didSet{
                self.invalidateIntrinsicContentSize()
            }
        }
    
        override func reloadData() {
            super.reloadData()
            self.invalidateIntrinsicContentSize()
        }
    }
    
  2. In Interface builder change the class of your tableView to MyOwnTableView (subclass UITableView).

  3. Set automatic row height for the table view.

        tableView.estimatedRowHeight = 60.0;
        tableView.rowHeight = UITableViewAutomaticDimension;
    

Second Approach: 1. Create a height constraint with any value for tableView and connect an IBOutlet that sets the tableView height.

  1. Set the height constraint's constant to tableView.contentSize.height after reloading the data.

    self.tableViewHeight.constant = self.tableView.contentSize.height
    
查看更多
登录 后发表回答