Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.