Created
December 28, 2019 16:13
-
-
Save DavidPiper94/41b33c6abf5696424dbd005e89e59b19 to your computer and use it in GitHub Desktop.
Example code for article about section index - Setup of UITableView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
// 1 | |
@IBOutlet weak var tableView: UITableView! | |
// 2 | |
let sectionTitles = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".map(String.init) | |
} | |
extension ViewController: UITableViewDataSource { | |
// 3 | |
func tableView(_ tableView: UITableView, | |
numberOfRowsInSection section: Int) -> Int { | |
10 | |
} | |
func tableView(_ tableView: UITableView, | |
cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) | |
cell.textLabel?.text = "Cell number \(indexPath.row)" | |
cell.detailTextLabel?.text = "In section \(sectionTitles[indexPath.section])" | |
return cell | |
} | |
// 4 | |
func numberOfSections(in tableView: UITableView) -> Int { | |
sectionTitles.count | |
} | |
func tableView(_ tableView: UITableView, | |
titleForHeaderInSection section: Int) -> String? { | |
sectionTitles[section] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment