【问题标题】:Swiftyjson - JSON to table view with alphabetical sectionsSwiftyjson - JSON 到带有字母部分的表格视图
【发布时间】:2016-04-21 21:58:46
【问题描述】:

一旦我通过 Alamofire 从我的 http 请求中取回 JSON,我就会得到本文底部提到的以下 swiftyjson JSON 对象。

如何提取这种格式的部分?

  • var 部分:[(索引:Int,长度:Int,标题:字符串)] = Array()
  • length 是每个部分下的联系人数量。给信件命名

然后如何提取每个字母下的每个对象数组?

所有这些都使用 swift。

目标是创建一个包含表格视图和字母部分的联系人列表。

如果我不清楚,请告诉我。

JSON 对象:

{
  "T" : [
    {
      "location" : "Shanghai",
      "firstName" : "User3",
      "id" : 3,
      "created_at" : "2016-04-19 12:54:23",
      "birthDate" : "2016-04-17",
      "email" : "user3@test.com",
      "profilePhotoPath" : "photos\/emptyProfilePhoto.jpg",
      "updated_at" : "2016-04-19 12:54:23",
      "lastName" : "Test"
    }
  ],
  "G" : [
    {
      "location" : "Jakaylaborough",
      "firstName" : "Lambert",
      "id" : 4,
      "created_at" : "2016-04-19 23:25:39",
      "birthDate" : "0000-00-00",
      "email" : "user4@test.com",
      "profilePhotoPath" : "photos\/emptyProfilePhoto.jpg",
      "updated_at" : "2016-04-19 23:25:39",
      "lastName" : "Gulgowski"
    }
  ],
  "W" : [
    {
      "location" : "East Sydni",
      "firstName" : "Jedediah",
      "id" : 5,
      "created_at" : "2016-04-19 23:25:39",
      "birthDate" : "0000-00-00",
      "email" : "user5@test.com",
      "profilePhotoPath" : "photos\/emptyProfilePhoto.jpg",
      "updated_at" : "2016-04-19 23:25:39",
      "lastName" : "Wehner"
    },
    {
      "location" : "East Rebeccaton",
      "firstName" : "Addison",
      "id" : 6,
      "created_at" : "2016-04-19 23:25:39",
      "birthDate" : "0000-00-00",
      "email" : "user6@test.com",
      "profilePhotoPath" : "photos\/emptyProfilePhoto.jpg",
      "updated_at" : "2016-04-19 23:25:39",
      "lastName" : "Weimann"
    }
  ]
}

【问题讨论】:

  • 你试过什么?使用 reduce 似乎是一个相对轻松的过程。
  • @JefferyThomas 我下面答案中的代码正在运行。唯一的问题是我的部分没有按字母顺序...
  • @JefferyThomas 最后一个问题已解决,答案编辑如下。如果有任何意见,请告诉我。

标签: json swift tableview swifty-json


【解决方案1】:

刚刚找到我的方式。在下面回答。

var sections : [(index: Int, length :Int, title: String)] = Array()
var contactsSorted: [JSON] = [JSON]()

// Delegate from my contact protocol
func didReceiveContacts(contacts: JSON){

    var index = 0

    for (key,subJson):(String, JSON) in contacts {

        let title = "\(key)"

        let newSection = (index: index, length: subJson.count, title: title)

        sections.append(newSection)

        contactsSorted.append(subJson)

        index += 1
    }
    // EDIT:Sort the sections by title to retrieve the alphabetical order
    sections.sortInPlace({ $0.title < $1.title })

    self.tableView.reloadData()
}

// tableView delegate methods

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return sections.count

}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    return sections[section].title

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return sections[section].length

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = self.tableView.dequeueReusableCellWithIdentifier("contactCell")! as! ContactTableViewCell

cell.nameLabel.text = contactsSorted[sections[indexPath.section].index].array![indexPath.row]["firstName"].string! + " " + contactsSorted[sections[indexPath.section].index].array![indexPath.row]["lastName"].string!

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多