2016-04-21 151 views
0

當我通過Alamofire從我的http請求中取回JSON時,我在本文底部提到了以下swiftyjson JSON對象。Swiftyjson - JSON到按字母順序排列的表格視圖

如何提取這種格式的部分?

  • VAR節:[(指數:中等,長度:中等,標題:字符串)] =陣列()
  • 長度爲下各部分接觸的數目。標題字母

然後我怎樣才能提取每個字母下的每個對象的數組?

所有使用swift的。

目標是創建一個聯繫人列表與一個tableview和按字母順序的部分。

如果我不清楚,請讓我知道。

JSON對象:

{ 
    "T" : [ 
    { 
     "location" : "Shanghai", 
     "firstName" : "User3", 
     "id" : 3, 
     "created_at" : "2016-04-19 12:54:23", 
     "birthDate" : "2016-04-17", 
     "email" : "[email protected]", 
     "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" : "[email protected]", 
     "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" : "[email protected]", 
     "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" : "[email protected]", 
     "profilePhotoPath" : "photos\/emptyProfilePhoto.jpg", 
     "updated_at" : "2016-04-19 23:25:39", 
     "lastName" : "Weimann" 
    } 
    ] 
} 
+0

你有什麼試過的?這似乎是一個使用reduce的相對無痛的過程。 –

+0

@JefferyThomas我在下面的答案中的代碼正在工作。唯一的問題是,我的部分不是按字母順序排列的... – sbkl

+0

@JefferyThomas上個問題已解決,並在下面編輯。讓我知道如果有任何評論。 – sbkl

回答

0

剛剛發現我的方式。下面回答。

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! 

}