ios - 如何在 UITableView 中按字母顺序对字典数组进行排序和分类,作为 swift 3.0 中的部分

标签 ios swift

我正在开发 swift,并且我有一系列字典,例如,

[ {"added": 2017-12-24, "first_name": Abdullah, "email": [email protected], "Last_name": Jaleel, "place": India, "salutation":Mr}, {"added": 2017-12-24, "first_name": Catherine, "email": [email protected], "Last_name": Rose, "place": India, "salutation":Mrs}, {"added": 2017-12-24, "first_name": Alok, "email": [email protected], "Last_name": Raj, "place": India, "salutation":Mr}, {"added": 2017-12-24, "first_name": Darwin, "email": [email protected], "Last_name": Jose, "place": India, "salutation":Mr}].

在这里,我想根据名字的字母顺序列出 UITableView 中的所有发言者。我可以按字母顺序对字典数组进行排序,但问题是我想在 UITableViewSection 中显示字母表,并且该部分仅包含以该字母表开头的名称(如 iPhone 的联系人屏幕)。

enter image description here 那么我可以按如下方式转换字典数组吗,

{ a: [{"added": 2017-12-24, "first_name": Abdullah, "email": [email protected], "Last_name": Jaleel, "place": India, "salutation":Mr}, {"added": 2017-12-24, "first_name": Alok, "email": [email protected], "Last_name": Raj, "place": India, "salutation":Mr}], c: [{"added": 2017-12-24, "first_name": Catherine, "email": [email protected], "Last_name": Rose, "place": India, "salutation":Mrs}], d:[{"added": 2017-12-24, "first_name": Darwin, "email": [email protected], "Last_name": Jose, "place": India, "salutation":Mr}] }.

是否可以像上面那样对字典数组进行分组?否则如何在 UITableViewSection 中按字母顺序列出所有发言者。请帮助我。

最佳答案

Is it possible to group the array of dictionary as above?

let users = [
    ["added": "2017-12-24", "first_name": "Abdullah", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f0c0f1e1a141a0d4e3f1a071e120f131a511c1012" rel="noreferrer noopener nofollow">[email protected]</a>", "Last_name": "Jaleel", "place": "India", "salutation": "Mr"],
    ["added": "2017-12-24", "first_name": "Catherine", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="71020110141a140340311409101c011d145f121e1c" rel="noreferrer noopener nofollow">[email protected]</a>", "Last_name": "Rose", "place": "India", "salutation": "Mrs"],
    ["added": "2017-12-24", "first_name": "Alok", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="15666574707e70672455706d74786579703b767a78" rel="noreferrer noopener nofollow">[email protected]</a>", "Last_name": "Raj", "place": "India", "salutation": "Mr"],
    ["added": "2017-12-24", "first_name": "Darwin", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81f2f1e0e4eae4f3b0c1e4f9e0ecf1ede4afe2eeec" rel="noreferrer noopener nofollow">[email protected]</a>", "Last_name": "Jose", "place": "India", "salutation": "Mr"]
]

func firstCharOfFirstName(_ aDict: [String:String]) -> Character {
    return aDict["first_name"]!.first!
}

let groupedUsers = Dictionary(grouping: users, by: firstCharOfFirstName)
print(groupedUsers)

--output:--
["C": 
[["added": "2017-12-24", "Last_name": "Rose", "place": "India", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="20535041454b455211604558414d504c450e434f4d" rel="noreferrer noopener nofollow">[email protected]</a>", "first_name": "Catherine", "salutation": "Mrs"]], 
"D": 
[["added": "2017-12-24", "Last_name": "Jose", "place": "India", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53202332363836216213362b323e233f367d303c3e" rel="noreferrer noopener nofollow">[email protected]</a>", "first_name": "Darwin", "salutation": "Mr"]], 
"A": 
[["added": "2017-12-24", "Last_name": "Jaleel", "place": "India", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0b3b0a1a5aba5b2f180a5b8a1adb0aca5eea3afad" rel="noreferrer noopener nofollow">[email protected]</a>", "first_name": "Abdullah", "salutation": "Mr"],  
["added": "2017-12-24", "Last_name": "Raj", "place": "India", "email": "spa<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cda8a6a8bffc8da8b5aca0bda1a8e3aea2a0" rel="noreferrer noopener nofollow">[email protected]</a>", "first_name": "Alok", "salutation": "Mr"]]]

然后您可以根据需要对每个组进行排序。

关于ios - 如何在 UITableView 中按字母顺序对字典数组进行排序和分类,作为 swift 3.0 中的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48002764/

相关文章:

ios - 从解析中查询图像和文本

ios - 在 ContainerViewController 类中以编程方式为 UIButton 设置新的背景图像,具体取决于它所在的父 View

ios - 如何在我所在位置的某个半径范围内找到使用我的应用程序的人

ios - 动态高度 UITableViewCell 内的动态高度 UICollectionView

swift - 原子引用计数和自动引用计数有什么区别?

ios - 是否可以添加一个 UIView 作为 UINavigationBar 的背景

ios - 在 iOS 13 中,从呈现模式中拉动刷新表格 View 不起作用

ios - Apple 二进制文件被拒绝 (2.16)

ios - SpriteKitNode 不动

arrays - Swift Playground 中的 2D 数组