2011-10-25 12 views
2

私はこのようなJSONデータを持っている:のUITableViewのセクション

[{"id":"3","name":"jason"},{"id":"4","name":"karen"}] 

私は、各ペアのセクション[ID、名]とテーブルビューを構築したいです。セクションのタイトルはidの値でなければなりません。各セクションの唯一のセルはの名前の値にする必要があります。

JSONデータを配列に解析し、表示するセクションの数を決定するには、[配列数]を使用するにはどうすればよいですか?

どうもありがとうございました。..

私の悪い英語を許して!

+0

ありがとう! – Jason

答えて

4

UITableViewDatasource次のメソッドを実装:UITableViewDelegateを実装して、あなたのセルの値を設定するには

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    return [[JSONArray objectAtIndex:section] objectForKey:@"id"]; 
} 

::セクションのタイトル値を設定するには

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [JSONArray count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.textLabel.text = [[JSONArray objectAtIndex:indexPath.section] objectForKey:@"name"]; // set the cell's text here 
    } 
    return cell; 
} 

参照チェックのためにUITableViewDataSourceおよびUITableViewDelegate

+0

あなたはいい男です。あなたのコードは非常に便利です。 – Jason

1

このプロジェクトを見てください。そのフレームワークは、JSONで動作します https://github.com/stig/json-framework/

このフレームワークのためのチュートリアルで発見することができます: http://iosdevelopertips.com/networking/iphone-json-flickr-tutorial-part-1.html

フレームワークは、NSStringのにカテゴリを追加します。そのカテゴリを使用すると、NSArrayの(あなたの例のためなどのオブジェクトのリスト)またはNSDictionaryの(いくつかのオブジェクトまたは構造)にJSONデータを解析することができます

#import "JSON.h" 
... 
NSString jsonString = //get your JSON from somewhere 
NSArray * array = [jsonString JSONValue]; 

私が何をすべきか、私はあなたの印象を与えることを願っあなたの目標を達成するために。さらに詳しい情報は、JSONプロジェクトのチュートリアルまたは上記のチュートリアルにあります。あなたのJSON配列からテーブルを構築する方法を

は答えである:私のエディタのMr.Wizard UITableView sections from JSON data

+0

ありがとう! – Jason

関連する問題