2016-04-21 18 views
0

Sobald ich die JSON von meiner HTTP-Anfrage über Alamofire zurückbekomme, bekomme ich das folgende swiftyjson JSON-Objekt, das am Ende dieses Beitrags erwähnt wird.Swiftyjson - JSON zur Tabellenansicht mit alphabetischen Abschnitten

Wie kann ich die Abschnitte in diesem Format extrahieren?

  • var Sektionen: [(index: Int, Länge: Int, Titel: String)] = Array()
  • Länge die Anzahl der Kontakt unter jedem Abschnitt zu sein. Titel der Buchstabe

Und dann wie kann ich jedes Array von Objekt unter jedem Buchstaben extrahieren?

All das mit Swift.

Ziel ist es, eine Liste von Kontakten mit einer Tabellenansicht und alphabetischen Abschnitten zu erstellen.

Bitte lassen Sie mich wissen, wenn ich nicht klar bin.

JSON-Objekt:

{ 
    "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

Was haben Sie versucht? Es scheint wie ein relativ schmerzloser Prozess mit reduzieren. –

+0

@JefferyThomas der Code in meiner Antwort funktioniert. Das einzige Problem ist, dass meine Abschnitte nicht in alphabetischer Reihenfolge sind ... – sbkl

+0

@JefferyThomas Letzte Ausgabe gelöst und die Antwort unten bearbeitet. Lass es mich wissen, wenn es einen Kommentar gibt. – sbkl

Antwort

0

gefunden Just my way. Antwort unten.

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! 

} 
Verwandte Themen