【发布时间】:2018-04-04 08:05:29
【问题描述】:
我正在从 Web 服务加载一些数据,我想在 UITableView 中使用这些数据。我可以为UITableView 设置一些初始值。但是,如果我将这些详细信息更新为 completionHandler 块,则不会发生更新。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURL *URL = [NSURL URLWithString:@"http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDQ60359.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
// Code that passes my web service data into an NSArray list
// self->sensors = locations;
self->sensors = [NSArray arrayWithObjects:@"key4",@"key5",@"key6",nil];
NSLog(@"Try to reload the tableView");
[self.tableView reloadData];
}
}];
[dataTask resume];
self->sensors = [NSArray arrayWithObjects:@"key1",@"key2",@"key3",nil];
}
我的应用程序启动,并在 UITableView 列表中显示 key1、key2 和 key3。 Key4、key5、key6 从不显示。 completionHandler 中的代码确实运行了,我已经通过 NSLogging 验证了它,我创建数组以放入 UITableView。
控制台显示“尝试重新加载tableView”。但是,我添加到 numberOfRowsInSection 和 cellForRowAtIndexPath 函数中的一些 NSLog 在“尝试重新加载 tableView”后没有显示。
如何让我的代码更新UITableView 以显示key4、key5、key6?
【问题讨论】:
-
确保传感器是可变的。
-
它会在控制台上打印
Try to reload the tableView吗? -
不是,我现在改了。但是我也注意到,在我调用 reloadData 之后,numberOfRowsInSection 和 cellForRowAtIndexPath 函数永远不会被调用。
-
Yes 显示“尝试重新加载tableView”。 numberOfRowsInSection 和 cellForRowAtIndexPath 中的 NSLog 在“尝试加载 tableView”后不显示。
-
确保 1) 仅在主线程上更新或重新加载 Tableview,2) Tableview 与数据源有连接
标签: ios objective-c uitableview