2015-07-20 58 views
0

我正在使用AFNetworking加載數據。在我的情況下,我加載數據到一個collectionview。 我加載我的自定義方法中的數據,並在裏面我重新加載collectionview數據。 而我在viewDidLoad方法中調用這個方法。爲什麼我問這是,它需要更多的時間來加載數據。我想這是因爲地方的我重裝的CollectionView data.I想知道,我可以像viewWillAppear中的另一種方法重載的CollectionView數據加快這一進程,或任何other.hope你你help.thank什麼是最好的地方重新加載UITableView或UICollectionView中的數據ios

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self loadcategoryData]; 

    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 
{ 
    [super viewWillAppear:animated]; 


    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 

} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
} 


- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
} 




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

- (void)loadcategoryData 
{ 
    post = nil; 
    NSString *mainurl = [NSString stringWithFormat:@"some url"]; 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; 
    [manager GET:mainurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 

     posts = (NSDictionary *)responseObject; 
     post = [NSMutableArray array]; 
     for(NSDictionary *all in posts) 
     { 
      Categories *category = [Categories new]; 
      category.title = [all objectForKey:@"catname"]; 
      category.firsturl = [all objectForKey:@"url"]; 

      [self.maincollectionView reloadData]; 
      //call for images 

      imagespost = nil; 
      NSString *imageUrl = [NSString stringWithFormat:@"%@", category.firsturl]; 
      AFHTTPRequestOperationManager *managerone = [AFHTTPRequestOperationManager manager]; 
      [managerone GET:imageUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 



       imagesposts = (NSDictionary *)responseObject; 
       NSArray *resultone = [imagesposts objectForKey:@"posts"]; 
       imagespost = [NSMutableArray array]; 
       if ([resultone count]) { 
        NSDictionary *firstpost = resultone[0]; 
//     Categories *newmogocat = [Categories new]; 


        NSDictionary *thumbnail_images = [firstpost objectForKeyedSubscript:@"thumbnail_images"]; 
        NSDictionary *thumbnail = [thumbnail_images objectForKey:@"thumbnail"]; 
        category.imageOneUrl = [NSString stringWithFormat:@"%@",[thumbnail objectForKey:@"url"]]; 
//     NSLog(@"%@", newmogocat.imageOneUrl); 
//     [imagespost addObject:newmogocat]; 

        [post addObject:category]; 

        [self.maincollectionView reloadData]; 
       } 

      } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
//     
//    UIAlertView *erroralert = [[UIAlertView alloc] initWithTitle:@"Something Wrong!" message:[NSString stringWithFormat:@"%@", error.localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
//    [erroralert show]; 

      }]; 


     } 

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

    }]; 
} 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return [post count]; 
} 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellidentifier" forIndexPath:indexPath]; 
// cell.layer.borderWidth = 2.0f; 
    cell.layer.masksToBounds = NO; 
    cell.layer.cornerRadius = 5.0; 
    cell.layer.borderColor = [UIColor lightGrayColor].CGColor; 
    cell.layer.shadowOffset = CGSizeMake(0, 1); 
    cell.layer.shadowRadius = 4.0; 
    cell.layer.shadowColor = [UIColor darkGrayColor].CGColor; 


    [cell addSubview:cell.maintitleLabel]; 

    Categories *cat = [post objectAtIndex:indexPath.row]; 
    [self.maincollectionView reloadInputViews]; 
    cell.maintitleLabel.text = [NSString stringWithFormat:@" %@ ", cat.title]; 
    [cell.maintitleLabel sizeToFit]; 






    NSString *mainstring = [NSString stringWithFormat:@"%@", cat.imageOneUrl]; 
    NSURL *url = [NSURL URLWithString:mainstring]; 
// 
    [cell.mainImageView setImageWithURL:url placeholderImage:nil]; 
// 



    return cell; 
} 

回答

0

您可以開始加載數據的最早時間是在init方法中。但說實話,大多數情況下,他們在彼此之後得到如此迅速的打電話,你可能甚至不會注意到其中的差異。但是,如果你有興趣,只是怪人它在這裏:

- (id)init 
{ 
    self = [super init]; 
    if (self) 
    { 
     [self loadcategoryData]; 
    } 
    return self; 
} 

此外,我不知道有多少數據你的下載,但如果要加載一個長長的清單,並花費很長的時間,那麼也許你應該考慮做「分頁」(即只下載數據的一個子部分,然後在用戶向下滾動頁面時請求更多)。

+0

我該怎麼做分頁,你能告訴我該怎麼做。到ios @ villy393 –

+0

那麼它更依賴於提供結果的服務器而不是你,例如你加載頁面然後請求20個結果,當用戶滾動某個點時,你需要下一個20結果,所以你需要想象用一些方法告訴服務器你想要更多的結果以及你想要的結果。檢查這個傢伙無限滾動項目的代碼:https://github.com/JigarM/Infinite-UIITableview-Scroll(注意它全部在Swift中) – villy393

+0

或者查看這個http://jslim.net/blog/2014/04/01/infinite-scroll-using-on-uitableview/ – villy393

相關問題