Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make UITableView not scrollable and adjust height to accommodate all cells

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?

like image 591
Allen Avatar asked Jun 06 '14 20:06

Allen


People also ask

How do I make a Tableview not scrollable?

You need to set the UITableView scroll to false , tableview. scrollEnabled = false; tableview.

Is UITableView scrollable?

UITableView scrolls back because it's content size is equal to it's frame (or near to it). If you want to scroll it without returning you need add more cells: table view content size will be large then it's frame.

Can UITableView scroll horizontal?

You can add a UITableView to a UIScrollView and have it scroll horizontally.

Should I use a scroll view or a UITableView?

For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views. The code of the table view data source often ends inside view controllers when it should go into a separate class.

Why use a UITableView instead of a Tableview?

Table views are more versatile than you might think. For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views.

What are the cells of UITableView?

The cells of UITableView are instances of UITableViewCell or its subclasses. It is the table view that adds, removes, and arranges cells in its view hierarchy. Table views reuse cells that go out of the screen to display new elements, so that:

How do I create a uitableviewcontroller in Android Studio?

Let’s create our View Controller. Right-click the View Controllers group, select New File and choose Cocoa Touch Class in the panel that appears. Then create a sub-class of UITableViewController. Since we’ll be using Storyboards in the app, you can leave the Also create XIB file option unchecked.


1 Answers

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 
like image 196
Learner Avatar answered Sep 21 '22 19:09

Learner