简体   繁体   English

如何使用Swift将字典数据分组

[英]How do I group Dictionary Data with Swift

I have my dictionary data in this format. 我的字典数据是这种格式的。

var data: [[String:AnyObject]] =

[
  [
    "id": "1",
    "title": "A Title",
    "Detail": "This is a String"
  ],      
  [
    "id": "2",
    "title": "A Title Again",
    "Detail": "This is a String"
  ],
  [
    "id": "3",
    "title": "B Title",
    "Detail": "This is a String"
  ],
  [
    "id": "4",
    "title": "B Title Again",
    "Detail": "This is a String"
  ]
]

Does anyone have an idea how I can group my tableView from [AZ] like the prototype image below with Swift. 有谁知道如何将[AZ]中的tableView分组,例如下面的Swift原型图像。

原型

This is a two step problem (that we'll solve in pure Swift). 这是一个两步问题(我们将在纯Swift中解决)。 First, we need to determine what sections we have (in this case, just A and B . This is fairly simple: 首先,我们需要确定我们拥有哪些部分(在这种情况下,只有AB这很简单:

var letters = Set<String>()

for element in data {
    var title = element["title"] as? String
    let letter = title?.substringToIndex(advance(title!.startIndex, 1))

    letters.insert(letter!)
}

We first create a set of String objects, so that each letter is represented just once. 我们首先创建一组String对象,以便每个字母仅代表一次。 To get the first character (in pure Swift), we must use substringToIndex and pass in a String.Index value using advance . 要获取第一个字符(在纯Swift中),我们必须使用substringToIndex并使用advance传递String.Index值。 As a side note, if you'd like to normalize the letters, append .uppercaseString before inserting the letter into letters . 作为一个侧面说明,如果你想正常化的字母,追加.uppercaseString将信投入前letters

print(letters) will then output [A, B] . 然后print(letters)将输出[A, B]

–– -

Secondly, we need to sort data by the title key. 其次,我们需要按title键对data进行排序。 Swift makes sorting quite easy: Swift使排序非常容易:

data.sort { element1, element2 in
    let title1 = element1["title"] as? String
    let title2 = element2["title"] as? String

    return title1 < title2
}

You can then arrange the data however you like, now knowing how to both sort by key, and detect which letters to show. 然后,您可以按自己喜欢的方式排列数据,现在知道如何既按键排序又可以检测要显示的字母。 Hope this helps! 希望这可以帮助!

You want the UITableViewDataSource API. 您需要UITableViewDataSource API。

The table that your image shows is a plain table view with titles and sections. 图像显示的表是带有标题和部分的普通表视图。

Some of the methods you want to implement are numberOfSectionsInTableView and titleForHeaderInSection . 您要实现的一些方法是numberOfSectionsInTableViewtitleForHeaderInSection For the scrollable index alongside the table, you want sectionIndexTitlesForTableView . 对于表旁边的可滚动索引,您需要sectionIndexTitlesForTableView All of these methods are described in the link above. 所有这些方法在上面的链接中进行了描述。

You may consider sorting your array and breaking it up by letter of the alphabet, but doing so can be expensive, so don't do it while your user is scrolling. 您可能会考虑对数组进行排序并按字母顺序将其分解,但是这样做可能会很昂贵,因此请不要在用户滚动时这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM