Add an Index in TableView

Wyn
2 min readApr 30, 2020

I want to create a tableView which is about a friend list. If there is a large mass of data. I’d like to have an index that sort list by the first letter in the tableView.

let names = ["Aaron", "Adam", "Adolph", "Bert", "Carey", "Colin", "Eden", "Edmund", "Elmo", "Gary", "Giles", "Harvie", "Hiram", "Jeff", "Jeremy", "Jo", "Kelly", "Nick", "Neil", "Noah", "Perry", "Philip", "Sampson", "Scott", "Sidney", "Simon", "Tim", "Todd", "Tracy", "Troy", "Vincent", "Walker", "Wayne", "Wythe", "Yasir", "York", "Zavier"]

First of all, I declare an empty dictionary named nameDict for sorting name list, an empty dictionary named nameSectionTitles for the section titles, and a constant named nameIndexTitles for the index.

var nameDict = [String: [String]]()
var nameSectionTitles = [String]()
let nameIndexTitles = ["A", "B", "C","D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

There are functions to deal with the number of sections and section titles in tableView.

override func numberOfSections(in tableView: UITableView) -> Int {return nameSectionTitles.count}override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {return nameSectionTitles[section]}

Set the index titles and fix the section index doesn’t exist any item.

override func sectionIndexTitles(for tableView: UITableView) -> [String]? {return nameIndexTitles}override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {guard let index = nameSectionTitles.firstIndex(of: title) else {return -1}return index}

Set numbers of rows in the section

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {let firstLetter = nameSectionTitles[section]guard let nameValues = namesDict[firstLetter] else {return 0}return nameValues.count}

Set the cell

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: “Cell”, for: indexPath)// Configure the celllet firstLetter = nameSectionTitles[indexPath.section]if let nameValues = namesDict[firstLetter] {cell.textLabel?.text = nameValues[indexPath.row]}return cell}

This is a very important part, that is about how to make the dictionary of the names. After collected key in the dictionary, sort them by the last two lines code.

func createNameDict() {for name in names {let firstLetter = String(name.prefix(1))if var nameValues = namesDict[firstLetter]{nameValues.append(name)namesDict[firstLetter] = nameValues} else {namesDict[firstLetter] = [name]}nameSectionTitles = [String](namesDict.keys)nameSectionTitles = nameSectionTitles.sorted(by: {$0 < $1})}}

There is a skill about debugging. Print the variables in the function. It'd let me know how the function works and how the variable change in a particular place.

let firstLetter = String(name.prefix(1))print(namesDict)if var nameValues = namesDict[firstLetter] {print(“in if”)print(namesDict)nameValues.append(name)namesDict[firstLetter] = nameValuesprint(“in end if”)print(namesDict)} else {print(“in else”)print(namesDict)namesDict[firstLetter] = [name]print(“in end else”)print(namesDict)}

Feel free to visit my Github for further details.

--

--

Wyn

Swift autodidact / Unity technical artist / Spark AR Studio creator / https://wenchung.wixsite.com/website