Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Introduction to Table Views Sorting Contacts Into Sections Displaying Sectioned Data

sectionTitle appears at the bottom of the section

'''ios extension Contact { var firstLetterForSort: String { return String(firstName.first!).uppercased() } }

extension ContactsSource { static var sortedUniqueFirstLetters: [String] { let firstLetters = contacts.map { $0.firstLetterForSort } let uniqueFirstLetters = Set(firstLetters) return Array(uniqueFirstLetters).sorted() }

static var sectionedContacts: [[Contact]] {
    return sortedUniqueFirstLetters.map { firstLetter in
        let filterdContacts = contacts.filter { $0.firstLetterForSort == firstLetter }
        return filterdContacts.sorted(by: { $0.firstName < $1.firstName } )

} override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { return sectionTitles[section] }

override func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections[section].count

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath)

    let contact = sections[indexPath.section][indexPath.row]

    cell.textLabel?.text = contact.firstName
    cell.imageView?.image = contact.image
    cell.detailTextLabel?.text = contact.lastName

    return cell


1 Answer

Figured it out. In the function for tableView I had selected the titleForFooterInSection function rather than the titleForHeaderInSection.