2016-04-21 10 views
0

私のhttpリクエストからAlamofireを介してJSONを取得すると、この記事の最後に記載されているswiftyjson JSONオブジェクトが取得されます。Swiftyjson - JSONからアルファベット順のテーブルビューへ

この形式でセクションを抽出するにはどうすればよいですか?

  • VARセクション:[(インデックス:INT、長さ:INT、タイトル:文字列)=配列()
  • 長さは、各セクションの下の接触の数です。文字のタイトルに

そして、どのように各文字の下にオブジェクトの各配列を抽出できますか?

すべてのものを迅速に使用しています。

目的は、テーブルビューとアルファベットセクションを持つ連絡先のリストを作成することです。

わからない場合は教えてください。

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! 

} 
関連する問題