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?
You need to set the UITableView scroll to false , tableview. scrollEnabled = false; tableview.
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.
You can add a UITableView to a UIScrollView and have it scroll horizontally.
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.
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.
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:
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.
Set tableView scrolling property to false.
First Approach:
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() } }
In Interface builder change the class of your tableView to MyOwnTableView (subclass UITableView).
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.
Set the height constraint's constant to tableView.contentSize.height after reloading the data.
self.tableViewHeight.constant = self.tableView.contentSize.height
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With