使用服务器返回的响应填充tableview单元格的代码查看

时间:2016-10-18 05:22:12

标签: ios objective-c iphone uitableview

连接类

-(NSDictionary *)getResponseFromSearchByRoutewithUrl:(NSString *)url  :(HttpCompletionBlock)completionHandler {
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [urlRequest setHTTPMethod:@"GET"];
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

NSDictionary *responseDictionary;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

NSURLSessionDataTask *task = [session dataTaskWithRequest: urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    //check if we encountered an error
    if(error != nil){
        NSLog(@"%@", [error localizedDescription]);
    }else{
        //get and check the HTTP status code
        NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];
        if (HTTPStatusCode != 200) {
            NSLog(@"HTTP status code = %ld", (long)HTTPStatusCode);
        }



        [task resume];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            if(data != nil){
                NSError *parseError = nil;
                NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
                NSLog(@"The response is - %@",responseDictionary);
                return completionHandler(responseDictionary,nil);

            }
            else {
                return completionHandler(nil,error);

            }
        }];
    }
}];
[task resume];
return responseDictionary;

}

具有表视图单元格Class

的ViewController
-(void)viewDidLoad
{
 _appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
      [[ConnectionManager sharedConnectionManager]getResponseFromSearchByRoutewithUrl:_urlString :^(NSDictionary *responseDictionary, NSError *error) {
    if(error == nil)
    {

     _appDelegate.flightsArray=[responseDictionary objectForKey:@"scheduledFlights"];
         NSLog(@"the flights array is%@ ", _appDelegate.flightsArray);
    }
    else
    {
        NSLog(@"Error ==  %@ ",error);
    }

}];
}

表格查看委托类方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _appDelegate.flightsArray.count;


}

请查看代码并告诉为什么行数为零......即使响应字典返回字典...

1 个答案:

答案 0 :(得分:0)

尝试以下代码

-(void)viewDidLoad
{
     _appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
     [[ConnectionManager sharedConnectionManager]getResponseFromSearchByRoutewithUrl:_urlString :^(NSDictionary *responseDictionary, NSError *error) {
     if(error == nil)
     {
          _appDelegate.flightsArray=[responseDictionary objectForKey:@"scheduledFlights"];
          NSLog(@"the flights array is%@ ", _appDelegate.flightsArray);
          [self performSelector:@selector(reloadTableview) withObject:nil afterDelay:0.2];
     }
     else
     {
          NSLog(@"Error ==  %@ ",error);
     }

     }];
}

- (void)reloadTableview{
     [self.tableview reloadData];
}