【问题标题】:how to load data , before user see the view in ios?如何在用户查看 ios 视图之前加载数据?
【发布时间】:2015-07-20 10:56:24
【问题描述】:

在我的情况下,我在标签栏控制器(类别和主页)中有两个视图,一个带有集合视图,一个带有表格视图。 在表格视图中,只有用户在其标签栏项目上点击(主页)时才会开始加载数据。我想在应用启动时加载数据。我正在使用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;
}

【问题讨论】:

    标签: ios uitableview uicollectionview viewdidload


    【解决方案1】:

    你要明白,数据只有在API请求获取数据完成后才会被加载和显示......

    这意味着您应该在屏幕出现之前准备好数据。仅当数据独立于用户操作或主数据时才有可能,并且您可以在应用启动时直接获取它。如果用户操作对获取的数据负责,那么您别无选择,只能等待并显示活动指示器。如果您决定这样做,您将找到相关的答案如何做到这一点。

    一切顺利...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      相关资源
      最近更新 更多