[英]how to load data , before user see the view in ios?
就我而言,我在標簽欄控制器中有兩個視圖(類別和主頁),一個具有集合視圖,一個具有表視圖。 在表格視圖中,僅當用戶點擊其選項卡欄項上的(主頁)時它才開始加載數據。我想在應用啟動時加載數據。 我正在使用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;
}
您已經了解到,僅在獲取數據的API請求完成后才會加載和顯示數據...
這意味着在屏幕出現之前您應該已經准備好數據。 僅當數據與用戶操作或主數據無關時,才有可能,並且您可以在應用啟動時直接獲取數據。 如果用戶操作負責提取的數據,則別無選擇,只能等待,並顯示活動指示器。 如果您決定執行此操作,則會找到相關的答案。
祝一切順利...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.