0

MAMP サーバーに接続してデータベースを選択し、内容を Xcode シミュレーターに表示するプログラムがあります。現在、2 つのタブがあり、両方のデータは同じです。各タブに表示するデータの種類をタブで分けたい。(1 つのタブにはワイン用ブドウの種類が表示され、他のタブにはワインの国が表示されます)

データをプルするクラス (NSObject のサブクラス) を作成し、次に各タブに必要なデータを保持する可変配列を持つ別のビュー コントローラーを作成する必要があると思いますが、どうすればよいですか? MutableArray を作成するにはどうすればよいですか?

JSON を使用してデータベースに接続する TableViewController.m のコードを次に示します
。NSURL *url = [NSURL URLWithString:@"http://[localhost]:8888/wine.php"]; // URL に合わせてこれを変更します。

NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
NSLog(jsonreturn); // Look at the console and you can see what the restults are

NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;

// In "real" code you should surround this with try and catch
NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
if (dict)
{
    rows = [[dict objectForKey:@"wine"] retain];


}
NSLog(@"Array: %@",rows);

[jsonreturn release];

}

次に、テーブルビュー用の別のメソッドを作成しました。

プラグマ マーク - テーブル ビュー データ ソース

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [行数]; }

// テーブル ビュー セルの外観をカスタマイズします。- (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];
}


// Configure the cell.
NSDictionary *dict = [rows objectAtIndex: indexPath.row];

//cell.textLabel.text = [dict objectForKey:@"id"];
//cell.textLabel.text = [dict objectForKey:@"wineColor"];
//cell.textLabel.text = [dict objectForKey:@"wineGrape"];
cell.textLabel.text = [dict objectForKey:@"wineCountry"];
cell.detailTextLabel.text = [dict objectForKey:@"id"];

return cell;

}

これにより、同じデータ (ワイン産地) がブドウと産地の両方のタブに表示されます。各タブに表示するデータを取得する可変配列を作成するにはどうすればよいですか?

4

1 に答える 1

0

独自のテーブルビューの tableView データソースとデリゲート メソッドを実装するには、2 番目の (ブドウ) ビュー コントローラーが必要ですが、別の配列は必要ありません。JSON 辞書を使用できますが、ブドウのセル内のラベルに適切なテキストを追加します。 (cellForRowAtIndexPath UITableView データソース メソッド内)。現在コメントアウトされている上記の行を使用して、「ブドウ」テーブルビューのセルにラベルを入力します。

//cell.textLabel.text = [dict objectForKey:@"wineColor"];
//cell.textLabel.text = [dict objectForKey:@"wineGrape"];

カスタム UITableViewCell を追加するか、字幕スタイルを使用して字幕を色に設定できます。

于 2013-04-16T19:30:57.297 に答える