【问题标题】:How do I get alphabetic tableView sections from an object如何从对象中获取字母 tableView 部分
【发布时间】:2019-10-15 14:54:22
【问题描述】:

这个问题之前已经发布过,比如Alphabetical sections in table table view in swift,但是当我尝试将它实现到我自己的代码中时,我真的无法理解它。

我有标准函数来获取字母部分:

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)
}

我有一个数组,它从应用程序的本地通讯录中收集了数据:var contactArray = [ExternalAppContactsBook]()ExternalAppContactsBook 看起来像这样:

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

我想根据lastName 在各自的部分下按字母顺序列出对象。我知道魔法发生在cellForRowAt,但我似乎无法正确处理。上面的文章将字符串映射到字典,但我需要一个对象,这使事情变得复杂。

我通过将lastNameobject 拆分为自己的array 来映射dictionary,如下所示:

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())}

但是我该如何使用它呢?

有人可以给我一些指导吗?

编辑 它以这种方式工作,但我不确定在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)
}

打印显示:

Section(字母:“H”,名称:[“Haro”,“Higgins”]),Section(字母:“T”,名称:[“Taylor”])

...等等。我认为实际填充tableView 时可能仍然存在问题。

【问题讨论】:

    标签: swift uitableview sections


    【解决方案1】:

    你要分组contactArraysections(这个名字就够了)必须成为数据源数组

    var sections = [Section]()
    

    你必须声明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})}
    

    根据我在链接问题中的回答,数据源和委托方法是

    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
    }
    

    【讨论】:

    • 谢谢。但是将grouping: lastNameArray 更改为对象contactArraysections = keys... 上出现错误“在'Sequence' 上引用实例方法'sorted()' 要求'ExternalAppContactsBook' 符合'Comparable'”。我删除了.sorted(),它给出了错误“无法将'[ExternalAppContactsBook]'类型的值转换为预期的参数类型'[String]'”旁注:Section()仅由struct Section { let letter : String let names : [String] }组成,这足够了吗?
    • 你必须采用Comparable。我更新了答案
    • 非常感谢您帮助我。即使在ExternalAppContactsBook 中实施更改后,我仍然遇到相同的错误:“无法将类型'[ExternalAppContactsBook]' 的值转换为预期的参数类型'[String]'”它是否与Section() 的方式有关好像? lastName 肯定是String,但是当添加前缀时它变成了Dictionary,对吧?也许这就是问题所在?
    • 答案中的代码应该可以工作。你从哪里得到错误?
    • 抱歉回复晚了,我一直在努力解决它。 :/ 无论如何,错误就在mapedSection = keys.map{Section(letter: $0, names: groupedDictionary[$0]!.sorted())} 线上。更具体地说,.sorted() 带有红色下划线。但是删除sorted() 仍然会产生错误。我已经在课程的不同部分尝试了代码,但没有成功。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多