我正在加载我希望在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”显示在控制台中。但是,在“尝试重新加载tableView”之后,我添加到numberOfRowsInSection
和cellForRowAtIndexPath
函数中的一些NSLog不显示。
如何让我的代码更新UITableView
以显示key4,key5,key6?
答案 0 :(得分:1)
尝试在主线程上重新加载tableview
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self->sensors = [[NSMutableArray alloc] initWithObjects: @"key1",@"key2",@"key3",nil];
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;
NSArray *newObjects = [NSArray arrayWithObjects:@"key4",@"key5",@"key6",nil];
[self->sensors addObjectsFromArray:newObjects];
NSLog(@"Try to reload the tableView");
[self.tableView reloadData];
}
}];
[dataTask resume];
}
答案 1 :(得分:0)