Last active
May 26, 2022 09:46
-
-
Save BrandonShega/6450e181231256867b7263782f591582 to your computer and use it in GitHub Desktop.
Table View Section Index List
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
//: Playground - noun: a place where people can play | |
import UIKit | |
import Foundation | |
import PlaygroundSupport | |
let allStates = ["Alaska", | |
"Alabama", | |
"Arkansas", | |
"American Samoa", | |
"Arizona", | |
"California", | |
"Colorado", | |
"Connecticut", | |
"District of Columbia", | |
"Delaware", | |
"Florida", | |
"Georgia", | |
"Guam", | |
"Hawaii", | |
"Iowa", | |
"Idaho", | |
"Illinois", | |
"Indiana", | |
"Kansas", | |
"Kentucky", | |
"Louisiana", | |
"Massachusetts", | |
"Maryland", | |
"Maine", | |
"Michigan", | |
"Minnesota", | |
"Missouri", | |
"Mississippi", | |
"Montana", | |
"North Carolina", | |
"North Dakota", | |
"Nebraska", | |
"New Hampshire", | |
"New Jersey", | |
"New Mexico", | |
"Nevada", | |
"New York", | |
"Ohio", | |
"Oklahoma", | |
"Oregon", | |
"Pennsylvania", | |
"Puerto Rico", | |
"Rhode Island", | |
"South Carolina", | |
"South Dakota", | |
"Tennessee", | |
"Texas", | |
"Utah", | |
"Virginia", | |
"Virgin Islands", | |
"Vermont", | |
"Washington", | |
"Wisconsin", | |
"West Virginia", | |
"Wyoming"] | |
struct State { | |
let name: String | |
} | |
class MyViewController: UIViewController { | |
let collation = UILocalizedIndexedCollation.current() | |
var sectionTitles: [String] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".characters.map { String($0) } | |
var sections: [[State]] = [] | |
var states: [State] = [] { | |
didSet { | |
sections = Array(repeating: [], count: sectionTitles.count) | |
let sortedStates = states.sorted { (lhs, rhs) -> Bool in | |
lhs.name < rhs.name | |
} | |
for state in sortedStates { | |
guard | |
let first = state.name.characters.first, | |
let sectionIndex = sectionTitles.index(of: String(first)) else { return } | |
sections[sectionIndex].append(state) | |
} | |
self.tableView.reloadData() | |
} | |
} | |
lazy var tableView: UITableView = { | |
let tableView = UITableView(frame: .zero) | |
tableView.delegate = self | |
tableView.dataSource = self | |
tableView.backgroundColor = .white | |
self.view.addSubview(tableView) | |
return tableView | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.backgroundColor = .white | |
tableView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true | |
tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true | |
tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true | |
tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true | |
tableView.translatesAutoresizingMaskIntoConstraints = false | |
states | |
loadStates() | |
} | |
func loadStates() { | |
states = allStates.map { .init(name: $0) } | |
} | |
} | |
extension MyViewController: UITableViewDelegate {} | |
extension MyViewController: UITableViewDataSource { | |
func numberOfSections(in tableView: UITableView) -> Int { | |
print(sections.count) | |
return sections.count | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
print(sections[section].count) | |
return sections[section].count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = UITableViewCell() | |
cell.textLabel?.text = sections[indexPath.section][indexPath.row].name | |
return cell | |
} | |
func sectionIndexTitles(for tableView: UITableView) -> [String]? { | |
return sectionTitles | |
} | |
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int { | |
return index | |
} | |
} | |
PlaygroundPage.current.liveView = MyViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment