我有从多次呈现给我的 UITableView 的服务返回的数据。我将填充我的表的代码放置在 connectionDidFinishLoading 委托中。这是该代码的正确位置吗?
我有一个 NSMutableData* receivedData; 在我的 .m 文件的顶部,我已经实现了正确的委托并覆盖了正确的方法。
我只想知道我在这里缺少什么,或者我可以做些什么来只在表格视图中查看我的 JSON 中的数据一次。
-(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");}