2015-07-20 73 views
0

在我的情況下,我有兩個視圖在標籤欄控制器(類別和家庭),一個集合視圖和一個表視圖。 在表格視圖中,當用戶在其標籤欄item.I上想要加載數據時,只有用戶點擊(Home)時纔開始加載數據。我使用的是AFNetworking,我在viewDidLoad中調用了我的數據加載方法。同樣的事情發生與收集view.please幫助我與this.hope你的幫助謝謝。我附加了我用來將數據加載到表視圖的部分代碼。如何在用戶在ios中查看視圖之前加載數據?

- (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

你得明白,數據只被載入和API請求獲取的數據完成後顯示...

這意味着您應該準備好數據出現在屏幕前。只有在數據與用戶操作或主數據無關的情況下才有可能,並且您可以在應用程序啓動時將其取出。如果用戶操作負責提取數據,則無法等待,只能顯示活動指示符。如果你決定這樣做,你會發現一個相關的答案如何做到這一點..

所有最好的...

相關問題