swift - Swiftyjson - JSON to table view with alphabetical sections -


once json http request through alamofire, following swiftyjson json object mentioned @ bottom of post.

how can extract sections in format?

  • var sections : [(index: int, length :int, title: string)] = array()
  • length being number of contact under each section. title letter

and how can extract each array of object under each letter?

all of using swift.

the objective create list of contacts tableview , alphabetical sections.

please let me know if i'm not clear.

json object:

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

just found way. answer below.

var sections : [(index: int, length :int, title: string)] = array() var contactssorted: [json] = [json]()  // delegate contact protocol func didreceivecontacts(contacts: json){      var index = 0      (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 sections title retrieve 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!  } 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -