2

NSArrayサーバーから取得した曲があります。それが私の生データソースです。配列には、バックエンドとしてNSDictionaryandを持つカスタム オブジェクトが含まれています。NSArray

を使用してフォーマットされたデータ ソースを実装するNSDictionaryことが賢明かどうか疑問に思っています。ディクショナリにはキーとしてセクション ヘッダーがあり、特定のキーの値には、NSArrayそのセクションの行を含む があります。

生データ ソースを反復処理し、辞書にアルファベット順に並べます。

これは確実な実装ではなく、非常にコストがかかると感じています。これよりも堅実な実装は他にありますか?

4

3 に答える 3

2

小さなテーブルの場合、 ではなくNSDictionaryを使用するのが一般的NSArrayです。これは、辞書は順序を保持しないためです (そして、おそらく継続的に並べ替えを行いたくないでしょう)。そのため、通常はセクションの配列があり、各セクション エントリには、少なくともセクション タイトルと行の配列があります。行の配列には、特定の行を表示するために必要な情報 (行のテキストなど) があります。

個々の行オブジェクトとセクション オブジェクトは、NSDictionaryオブジェクト自体として実装できます (JSON や XML からデータを解析する場合は、それが最も簡単な場合もあります) が、私は通常、独自のオブジェクトRowSectionオブジェクトを定義します。たとえば、次のようになります。

@interface Row : NSObject

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *subtitle;

@end

@interface Section : NSObject

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSMutableArray *rows;

@end

次に、テーブルビューコントローラーにNSArrayはセクションがあります:

@property (nonatomic, strong) NSMutableArray *sections;

そして、私はそれを次のように設定します:

self.sections = [NSMutableArray array];

Section *sectionObject;

sectionObject = [[Section alloc] initWithTitle:@"Marx Brothers" rows:nil];
[sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Chico"   subtitle:@"Leonard Marx"]];
[sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Harpo"   subtitle:@"Adolph Marx"]];
[sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Groucho" subtitle:@"Julius Henry Marx"]];
[sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Zeppo"   subtitle:@"Herbert Manfred Marx"]];
[self.sections addObject:sectionObject];

sectionObject = [[Section alloc] initWithTitle:@"Three Stooges" rows:nil];
[sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Moe"   subtitle:@"Moses Harry Horwitz"]];
[sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Larry" subtitle:@"Louis Feinberg"]];
[sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Curly" subtitle:@"Jerome Lester \"Jerry\" Horwitz"]];
[self.sections addObject:sectionObject];

そして、私は典型的なUITableViewDataSource方法を持っています:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    Section *sectionObject = self.sections[section];
    return [sectionObject.rows count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    Section *sectionObject = self.sections[section];
    return sectionObject.title;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    Section *sectionObject = self.sections[indexPath.section];
    Row *rowObject = sectionObject.rows[indexPath.row];

    cell.textLabel.text = rowObject.title;
    cell.detailTextLabel.text = rowObject.subtitle;

    return cell;
}

より大きなデータベース データ ドリブン テーブルの場合、データを配列に保持せずにコア データまたは SQLite を使用する場合がありますが、考え方は同じです。テーブル ビュー コントローラーのコードを一目瞭然にし、データ実装の詳細から隔離するクラスがあることSectionを確認してください。Row

于 2013-02-23T20:33:17.930 に答える
1

RestKitを試しましたか?必要なのは、jsonでエンコードされたオブジェクトのソースを提供し、モデルクラスを作成することだけです。

于 2013-02-23T16:46:41.963 に答える
0

モデル クラスを使用して、サーバーからのデータを使用してこのモデル クラスのオブジェクトを作成できます。次に、モデル クラス オブジェクトの配列を UITableView のソースとして使用します。データモデルと関係を非常に簡単に作成できるcore-data
を検討することもできます。ただし、多くの追加機能が付属しているため、余分な荷物になる可能性があります.

于 2013-02-23T17:24:04.360 に答える