如何在用户看到ios中的视图之前加载数据?

时间:2015-07-20 10:56:24

标签: ios uitableview uicollectionview viewdidload

在我的情况下,我在标签栏控制器(类别和主页)中有两个视图,一个具有集合视图,另一个具有表视图。 在表格视图中,当只有用户点击其标签栏项目时,它开始加载数据。我想在应用程序启动时加载数据。我正在使用AFNetworking,我在viewDidLoad中调用了我的数据加载方法。同样的事情发生在集合视图中。请帮我解决这个问题。请帮忙谢谢。我附加了部分代码,用于将数据加载到表视图中。

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.homeTableView];
//    [self homeviewData];

    SWRevealViewController *revealController = self.revealViewController;
    if (revealController) {
        [self.sidebarButton setTarget:self.revealViewController];
        [self.sidebarButton setAction:@selector(revealToggle:)];
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    }
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [self homeTableView];
}


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

- (void)homeviewData
{
    homepost = nil;
    NSString *mogohomeWebserviceUrl = [NSString stringWithFormat:@"some url"];
    AFHTTPRequestOperationManager *homeManager = [AFHTTPRequestOperationManager manager];
    [homeManager GET:mogohomeWebserviceUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        homeposts = (NSDictionary *)responseObject;
        homepost = [NSMutableArray array];
        NSArray *results = [homeposts objectForKey:@"posts"];
        for (NSDictionary *all in results)
        {
            Home *sweetHome = [Home new];
            sweetHome.homeImage = [NSString stringWithFormat:@"%@", [all objectForKey:@"thumbnail"]];
            sweetHome.homeTitle = [NSString stringWithFormat:@"%@", [all objectForKey:@"title"]];
            sweetHome.homewebUrl = [NSString stringWithFormat:@"%@", [all objectForKey:@"url"]];
            sweetHome.modifiedTime = [NSString stringWithFormat:@"%@", [all objectForKey:@"modified"]];

            [homepost addObject:sweetHome];
            [self.homeTableView reloadData];

        }

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

    }];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [homepost count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"homereuseIdentifier"];
    Home *mogoHome = [homepost objectAtIndex:indexPath.row];

    NSString *homeimageurlname = [NSString stringWithFormat:@"%@", mogoHome.homeImage];
    NSURL *homeimageurl = [NSURL URLWithString:homeimageurlname];

    cell.hometitleLabel.text = mogoHome.homeTitle;
    [cell.homeimageView setImageWithURL:homeimageurl placeholderImage:nil];
    cell.timeLabel.text = [[mogoHome.modifiedTime componentsSeparatedByString:@" "] objectAtIndex:1];
    return cell;
}

1 个答案:

答案 0 :(得分:1)

您必须明白,只有在完成获取数据的API请求后才会加载和显示数据......

这意味着您应该在屏幕出现之前准备好数据。仅当数据独立于用户操作或主数据时才可以执行此操作,您可以在App Launch上直接获取数据。如果用户操作负责获取的数据,则除了等待并且显示活动指示器之外别无选择。如果您决定这样做,您将找到相关答案如何做到这一点..

一切顺利......