-
Notifications
You must be signed in to change notification settings - Fork 3
iOS UITableView
A view controller at a minimum must adhere to the UITableViewDelegate
and UITableViewDatasource
protocols.
UIRefreshControl
s are designed to work only with UITableView
s or UITableViewController
s.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
/*
A UIRefreshControl sends a `valueChanged` event to signal
when a refresh should occur.
*/
refreshControl.addTarget(self, action: #selector(ViewController.refresh), for: .valueChanged)
tableView.addSubview(refreshControl)
}
@objc private func refresh() {
...
// Hide refresh control
if refreshControl.isRefreshing {
refreshControl.endRefreshing()
}
}
To allow AutoLayout to dynamically size UITableViewCell
instances, the following two lines are necessary in a view controller.
tableView.estimatedRowHeight = 100.0
tableView.rowHeight = UITableViewAutomaticDimension
Usually, cell height is defined by the tableView:heightForRowAtIndexPath
method from the UITableViewDelegate
protocol.
estimatedRowHeight
gives the tableView
an approximate height for each cell. This value is used to calculate scroll bar heights and positioning, among other things.
tableView.estimatedRowHeight = 100.0
Automatic sizing uses AutoLayout constraints to calculate height.
tableView.rowHeight = UITableViewAutomaticDimension
Both estimatedRowHeight
and UITableViewAutomaticDimension
must be set.