【问题标题】:Updating data in UITableView after asynchronous load异步加载后更新 UITableView 中的数据
【发布时间】: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”。但是,我添加到 numberOfRowsInSectioncellForRowAtIndexPath 函数中的一些 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


【解决方案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];


}

【讨论】:

  • 我刚刚尝试过,包括在 dispatch_async 块内设置 self->sensors 数组,没有任何变化。 UITableView 从不尝试重新加载。
  • 检查tableview数据源是否绑定正确。如果要将新密钥添加到现有数组,self->sensors 应该从 API 附加新密钥,因此将 Array 设为 NSMutableArray。尝试在 NumberOfrows tableview 方法上保留一个断点并检查它是否被调用?
  • 原来这是问题,而不是线程。我那里没有参考插座。
【解决方案2】:

原来我的问题不在于线程。一旦我添加了这个引用出口,问题中的代码就可以工作了:

从 storyboard 中的 UITableView 拖动到 ViewController 接口的控件添加了这一行:

@property (weak, nonatomic) IBOutlet UITableView *tableView;

我之前手动将其放入,但没有在情节提要中添加引用插座。我忘记了这一步。完成这一步后,我不必担心我从哪个线程调用 UITableView reloadData。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 2021-09-05
    • 1970-01-01
    • 2013-04-07
    相关资源
    最近更新 更多