iOS刷新JSON表

时间:2015-03-10 05:04:23

标签: ios objective-c json

我在刷新JSON Feed并更新相关表和子视图时遇到问题。我正在使用它在应用程序首次启动时调用JSON:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navigationController.delegate = self;

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:THE_URL]];
NSURLConnection* topAppsConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (topAppsConnection)
    self.topAppsData = [NSMutableData data];

[self.window addSubview:loadingView];
[self.window makeKeyAndVisible];

return YES;

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSString* topAppsString = [[NSString alloc] initWithData:topAppsData encoding:NSUTF8StringEncoding];
    NSLog(@"topAppsString: %@",topAppsString);
self.topAppsData = nil;

@try {
    RootViewController* rootViewController = (RootViewController*)[navigationController.viewControllers objectAtIndex:0];
    rootViewController.topApps = [topAppsString JSONValue];
    [self.window addSubview:navigationController.view];
}
@catch (NSException * e) { }

}

我正在使用以下内容在导航控制器上进行刷新:

- (void)reloadData {

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:THE_URL]];
NSURLConnection* topAppsConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[self.tableView reloadData];

if (self.refreshControl) {

    [self.refreshControl endRefreshing];
}

}

对于我缺少什么来刷新的想法?谢谢!

1 个答案:

答案 0 :(得分:1)

您的回复在哪里?在NSURLRequest之后添加以下代码:

NSData *response = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:nil error:nil];

NSArray *responseArray=[NSJSONSerialization JSONObjectWithData:response options:0 error:nil];
NSLog(@"%@",responseArray);

并将此数组用于填充表。