Json数据未在uitableview ios中更新



我正在使用故事板构建一个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];
            }

首先尝试从服务器调用数据并获取数据。之后,用这样的获取数据加载表视图。

 -(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];
    }

最新更新