Skip to content

Instantly share code, notes, and snippets.

@DavidPiper94
Created December 28, 2019 16:13
Show Gist options
  • Save DavidPiper94/41b33c6abf5696424dbd005e89e59b19 to your computer and use it in GitHub Desktop.
Save DavidPiper94/41b33c6abf5696424dbd005e89e59b19 to your computer and use it in GitHub Desktop.
Example code for article about section index - Setup of UITableView
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