How do I get alphabetic tableView sections from an object

时光毁灭记忆、已成空白 提交于 2020-01-03 01:53:18

问题


This question has previously been posted, like Alphabetical sections in table table view in swift, but I can't really wrap my head around it when I try to implement it into my own code.

I have the standard functions to get the alphabetic sections in place:

func numberOfSections(in tableView: UITableView) -> Int {
    return collation.sectionTitles.count
}

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

func sectionIndexTitles(for tableView: UITableView) -> [String]? {
    return collation.sectionIndexTitles
}

func tableView(tableView: UITableView!, sectionForSectionIndexTitle title: String!, atIndex index: Int) -> Int {
    return collation.section(forSectionIndexTitle: index)
}

I have an array that has collected data from the local contact book on the app: var contactArray = [ExternalAppContactsBook](). ExternalAppContactsBook looks like this:

class ExternalAppContactsBook {
    var firstName = ""
    var lastName = ""
    var phoneNumber = ""
    var company = ""
}

I would like to list the object in alphabetical order based on lastName under their respective sections. I get that the magic happens in cellForRowAt but I can't seem to get it right. The article above maps a string to a dictionary, but I need an object, which complicates things.

I have mapped a dictionary by breaking out lastName from the object into its own array like this:

var lastNameArray = [String]()

for index in self.contactArray {
    lastNameArray.append(index.lastName)
}

let groupedDictionary = Dictionary(grouping: lastNameArray, by: {String($0.prefix(1))})
let keys = groupedDictionary.keys.sorted()
var mapedSection = [Section]()
mapedSection = keys.map{Section(letter: $0, names: groupedDictionary[$0]!.sorted())}

But how do I use it?

Could someone give me a few pointers to get started?

EDIT It works this way, but I'm unsure if it's gonna work to arrange the object in the tableview:

for index in self.contactArray {
    lastNameArray.append(index.lastName)
}

if let c = self.contactArray {
    let groupedDictionary = Dictionary(grouping: lastNameArray, by: {String($0.prefix(1))})
    let keys = groupedDictionary.keys.sorted()
    var mapedSection = [Section]()
    mapedSection = keys.map{Section(letter: $0, names: groupedDictionary[$0]!.sorted())}
    print("☎️", mapedSection)
}

The print shows:

Section(letter: "H", names: ["Haro", "Higgins"]), Section(letter: "T", names: ["Taylor"])

...and so on. I think there still might be a problem when actually populating the tableView.


回答1:


You have to group contactArray and sections (this name is sufficient) must become the data source array

var sections = [Section]()

and you have to declare Section

struct Section {
    let letter : String
    let people : [ExternalAppContactsBook]
}

let groupedDictionary = Dictionary(grouping: contactArray, by: {String($0.lastName.prefix(1))})
let keys = groupedDictionary.keys.sorted()
sections = keys.map{Section(letter: $0, people: groupedDictionary[$0]!.sorted(by: {$0.lastName < $1.lastName})}

According to my answer in the linked question the datasource and delegate methods are

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

func numberOfSectionsInTableView(tableView: UITableView) -> Int{
    return sections.count
}

func sectionIndexTitles(for tableView: UITableView) -> [String]? {
    return sections.map{$0.letter}
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sections[section].letter
}


来源:https://stackoverflow.com/questions/58397502/how-do-i-get-alphabetic-tableview-sections-from-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!