何时在UITableViewController中加载数据?

Chr*_*ING 8 uitableview ios

我目前正在加载来自JSONviewDidLoad方法中的服务的数据UITableViewController.问题是数据需要时间来检索和解析,视图需要时间来创建.加载此数据的最佳位置在哪里?我假设在创建视图后有一个钩子在某处加载数据.通过这样做,我将能够UIActivityIndicatorView在最终视图中使用一些.谢谢

Chr*_*ING 6

最后这里有一个基于注释的解决方案:在viewDidLoad中启动一个线程来获取数据而不阻塞所有:

- (void) viewDidLoad
{
    dataLoaded = NO;

    [self initSpinner];
    [self launchLoadData];
...
}

-(void)launchLoadData {
    NSLog(@"Launching thread");
    [NSThread detachNewThreadSelector:@selector(loadData) toTarget:self withObject:nil];
}

- (void) loadData {
    dataLoaded = NO;
    NSLog(@" thread launched");
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [self loadDataFromURL:nil];
    dataLoaded = YES;
    [self.tableView reloadData];
    [pool release];
}

- (void)loadDataFromURL:(NSString*)url {
    // start the spinner to show that loading may be time consuming...
    [NSThread detachNewThreadSelector: @selector(spinBegin) toTarget:self withObject:nil];
    JSONLoader *loader = [[JSONLoader alloc] init];
    self.accounts = [loader getAccountsFromURL:@"http://foo/bar/repository.json"];
    [loader release];
    //[NSThread sleepForTimeInterval:3];
    [NSThread detachNewThreadSelector: @selector(spinEnd) toTarget:self withObject:nil];
}
Run Code Online (Sandbox Code Playgroud)

并使用该标志显示或不显示表中的数据.从线程调用时,tableView reloadData将执行其余操作.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataLoaded) return [self.accounts count];
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*lan 1

我认为您想问的是在 UITableView 中显示来自 Web 服务的数据的工作流程。

这是我的建议:

  • 你为你的 JSON 文件viewDidLoad创建了一个NSURLRequest。还向当前视图添加一个加载视图(我使用UIView带有黑色背景(0.5 alpha)的a,加上标签和 UIActivityIndi​​cator)。在此方法中,您还可以将 BOOL ivar(您需要将其添加到标头中)设置loaded为 NO。

  • 您可以将NSURLRequest数据合并为可变数据对象。

  • 完成后NSURLRequest,将其数据转换为字符串,并将 JSON 解析为某种类型的数组(如果需要,也可以将其解析为字典)。用同样的方法删除加载视图,并将布尔值更改loaded为 YES。然后你告诉 tableView 重新加载它的数据:[self.tableView reloadData];

这就是神奇发生的地方......在你的表视图方法中

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (loaded) return [myArrayOfJSONObjects count];
    return 0; // Will only return 0 if the data is not downloaded
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    if (loaded) {
        cell.textLabel.text = [myArrayOfParsedJSONObjects objectAtIndex:row];
        //Anything else you want to set
    }
    else {
        //Do nothing :) - you shouldn't reach this else anyway because your numberOfRows method should stop it
    }
}
Run Code Online (Sandbox Code Playgroud)