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");}