I have a UIViewController
holding one UITableView
with 3 sections. Also this UIViewController implements UISearchResultsUpdating
for searching in that UITableView
. The UIViewController
looks like this:
class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
var searchController = UISearchController(searchResultsController: nil)
@IBOutlet weak var tableView: UITableView!
private struct TableViewSections {
static let sectionOne = 0
static let sectionTwo = 1
static let sectionThree = 2
}
override func viewDidLoad() {
super.viewDidLoad()
self.setupTableView()
self.setupSearchController()
}
private func setupSearchController() {
self.searchController.searchResultsUpdater = self
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.placeholder = "Search"
self.searchController.searchBar.autocapitalizationType = .sentences
self.searchController.hidesNavigationBarDuringPresentation = false
if #available(iOS 11.0, *) {
self.navigationItem.searchController = self.searchController
} else {
self.tableView.tableHeaderView = self.searchController.searchBar
}
}
private func setupTableView() {
self.tableView.delegate = self
self.tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// In each of these cases i will do the work that is needed to return the correct number of rows
// Just for demonstration i return a random int in every case
if self.searchController.isActive {
switch section {
case TableViewSections.sectionOne:
return 10
case TableViewSections.sectionTwo:
return 20
case TableViewSections.sectionThree:
return 30
default:
return 0
}
} else {
switch section {
case TableViewSections.sectionOne:
return 15
case TableViewSections.sectionTwo:
return 25
case TableViewSections.sectionThree:
return 35
default:
return 0
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// In each of these cases i will do the work that is needed to show the correct content
// Just for demonstration i return UITableViewCell() in every case
switch indexPath.section {
case TableViewSections.sectionOne:
if self.searchController.isActive {
return UITableViewCell()
} else {
return UITableViewCell()
}
case TableViewSections.sectionTwo:
if self.searchController.isActive {
return UITableViewCell()
} else {
return UITableViewCell()
}
case TableViewSections.sectionThree:
if self.searchController.isActive {
return UITableViewCell()
} else {
return UITableViewCell()
}
default:
return UITableViewCell()
}
}
// MARK: UISearchResultsUpdating
func updateSearchResults(for searchController: UISearchController) {
// do the search work here...
}
}
You can see the difference between tableView(_:numberOfRowsInSection:)
and cellForRow(at:)
.
My Question now is: What is recommended way to do so? Is the better solution tableView(_:numberOfRowsInSection:)
, cellForRow(at:)
or is there even a better solution?