ios - UITableView 方法在 ViewDidLoad 之前调用

标签 ios objective-c iphone uitableview viewdidload

我正在尝试创建一个简单的天气应用程序,它使用 JSON 从 OpenweatherMap 获取数据并在 UITableView 中打印出来。但是,当我在下面执行此代码并在 numberOfRowsInSection 方法中设置断点时,它返回 0 行。在 numberOfRowsInSection 方法之后调用了 viewDidLoad 方法,这就是 threeHoursForecast 数组为空的原因。有人可以帮我解决这个问题吗?

static NSString * const BaseURLString = @"http://api.openweathermap.org/data/2.5/forecast?q=Houston";

@interface HPViewController ()

@end

@implementation HPViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.threeHoursForecast = [[NSMutableArray alloc] init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager POST:BaseURLString parameters:nil
          success:^(AFHTTPRequestOperation *operation, id responseObject) {

              NSArray *data = [responseObject objectForKey:@"list"];

              for (NSDictionary *forecastPerThreeHours in data)
                  [self.threeHoursForecast addObject:[[forecastPerThreeHours valueForKey:@"main"] valueForKey:@"temp"]];
          }

          failure:^(AFHTTPRequestOperation *operation, NSError *error) {

              NSLog(@"Error: %@", error);
          }];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.threeHoursForecast count];
}

最佳答案

您可以在调用完成处理程序后重新加载数据,这将解决问题。

[manager POST:BaseURLString parameters:nil
          success:^(AFHTTPRequestOperation *operation, id responseObject) {

              NSArray *data = [responseObject objectForKey:@"list"];

              for (NSDictionary *forecastPerThreeHours in data)
                  [self.threeHoursForecast addObject:[[forecastPerThreeHours valueForKey:@"main"] valueForKey:@"temp"]];

              //Add this line 
              [self.TableView reloadData];
          }

          failure:^(AFHTTPRequestOperation *operation, NSError *error) {

              NSLog(@"Error: %@", error);
          }];

关于ios - UITableView 方法在 ViewDidLoad 之前调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156187/

相关文章:

ios - 在 UITests 中获取 UISwitch 状态/值

ios - nstableview 拖放自定义单元格 View

ios - UITab 栏 - 选择指示器图像 - 更改位置

iphone - 添加 subview 后 superview 和parentviewcontroller nil

ios - 如何增加主视图的 subview 的大小

ios - danielgindi/iOSCharts 库 : x axis label in horizontal bar chart

ios - 浏览 UIViewControllers/UIView — 代码和概念问题

iOS:使用尺寸类别的 ios 应用程序所需的最少图像资源

ios - 是否可以通过 iOS 应用程序确认电话是否已接听?

iphone - 需要确认继承 UIAlertView 是合法的