Skip to content

Instantly share code, notes, and snippets.

@wb-towa
Forked from DavidPiper94/Setup.swift
Created May 26, 2022 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wb-towa/6fcc43b9e59a159d21a83dcaea635628 to your computer and use it in GitHub Desktop.
Save wb-towa/6fcc43b9e59a159d21a83dcaea635628 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