1

UITableViewに数回レンダリングされるサービスからデータが返されました。テーブルに入力するコードをconnectionDidFinishLoadingデリゲートに移動しました。これはそのコードの正しい配置ですか?

NSMutableData*receivedDataがあります。.mファイルの先頭にあり、正しいデリゲートを実装し、正しいメソッドをオーバーライドしました。

ここで何が欠けているのか、またはテーブルビューでJSONのデータを1回だけ表示するために何ができるのかを知りたいだけです。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {    
[receivedData setLength:0];
NSLog(@"%@",response);}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"Succeeded! Received %d bytes of data",[data length]);
receivedData = [[NSMutableData alloc] init];
[receivedData appendData:data];}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *error = nil;
// Get the JSON data from the website
id result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];

if ([result isKindOfClass:[NSArray class]]) {

    for (NSArray *item in result) {
        NSArray *category = [item valueForKey:@"CategoryName"];
        [dataArray addObject:category];
    }
}
else {
    NSDictionary *jsonDictionary = (NSDictionary *)result;

    for(NSDictionary *item in jsonDictionary)
        NSLog(@"Item: %@", item);
}

[self.tableView reloadData];

NSLog(@"Finished");}
4

1 に答える 1

1

エラーのある行があると思います

receivedData = [[NSMutableData alloc] init];

データを受け取るたびに、オブジェクトを再度初期化しています。

JSON部分には、このページhttp://nsscreencast.com/episodes/6-afnetworkingをお勧めします。

于 2012-09-14T04:10:14.587 に答える