我正在使用storyboards构建一个iOS文章阅读应用程序。我正在使用AFNetworking库来解析表格视图中的json。
我遇到一个问题,即更新的文章没有在表格视图中显示,这意味着如果在json中添加了新文章但在我的表格视图中没有更新。
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView reloadData];
x=2;
tempJson = [[NSMutableArray alloc] init];
[self.tableView reloadData];
NSString *jsonLink=[NSString stringWithFormat:@"http://cccccc.com/json.php?token=hashg156349&page=1"];
NSURL *url = [[NSURL alloc] initWithString:jsonLink];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30.0];
[request setTimeoutInterval:120];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *jsonArray = (NSArray *)responseObject;
dispatch_async(dispatch_get_main_queue(), ^{
for (NSDictionary *dic in jsonArray) {
Json *json = [[Json alloc] initWithDictionary:dic];
[tempJson addObject:json];
}
self.jsons = [[NSArray alloc] initWithArray:tempJson];
// tempNinjas = nil;
[self.tableView reloadData];
self.tableView.separatorColor = [UIColor colorWithRed:190/255.0 green:190/255.0 blue:190/255.0 alpha:1.0];
});
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
}
答案 0 :(得分:0)
首先尝试从服务器调用您的数据并获取数据。之后,使用您提取的数据加载表格视图。
-(void)viewWillAppear{
x=2;
tempJson = [[NSMutableArray alloc] init];
NSString *jsonLink=[NSString stringWithFormat:@"http://cccccc.com/json.php?token=hashg156349&page=1"];
NSURL *url = [[NSURL alloc] initWithString:jsonLink];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30.0];
[request setTimeoutInterval:120];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *jsonArray = (NSArray *)responseObject;
dispatch_async(dispatch_get_main_queue(), ^{
for (NSDictionary *dic in jsonArray) {
Json *json = [[Json alloc] initWithDictionary:dic];
[tempJson addObject:json];
}
self.jsons = [[NSArray alloc] initWithArray:tempJson];
// tempNinjas = nil;
self.tableView.separatorColor = [UIColor colorWithRed:190/255.0 green:190/255.0 blue:190/255.0 alpha:1.0];
});
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
[self.tableView reloadData];
}