2014-09-18 1 views
0

Je construis une application de lecture d'articles. J'utilise la bibliothèque tierce AFNetworking pour récupérer des données JSON dans UITableView. Disons que le lien Json est www.example.com & page = 1 donne 1-10 articles et www.example.com & page = 2 donne 11-20 articles et ainsi de suite.Appel de la méthode scrollviewdidscroll dans uitableview?

J'ai implémenté la pagination et la méthode scrollViewDidScroll signifie que lorsque l'utilisateur fait défiler, il donne l'article suivant.

Je suis confronté à un problème lors du lancement de l'application et UITableView charger la méthode scrollViewDidScroll appelé trois fois, mais l'appel attendu une fois. J'utilise une variable d'incrémentation pour la pagination dans la méthode scrollViewDidScroll comme je l'appelle trois fois et la valeur x va à 3 et donne 30 articles.

Lorsque l'utilisateur fait à nouveau défiler, il donne les 30 prochains articles. Je n'arrive pas à comprendre pourquoi la méthode scrollViewDidScroll est appelée trois fois lorsque l'application est lancée.

ceci est mon code:

 - (void)viewDidLoad 
      { 
       [super viewDidLoad]; 
       tempJson = [[NSMutableArray alloc] init]; 
       [self loadNinjas]; 
      } 

      - (void)loadNinjas { 

      NSString *jsonLink=[NSString stringWithFormat:@"www.example.com&page=%d",x]; 
      NSURL *url = [[NSURL alloc] initWithString:jsonLink]; 
      NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
      AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    operation.responseSerializer = [AFJSONResponseSerializer serializer]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSArray *jsonArray = (NSArray *)responseObject; 
     for (NSDictionary *dic in jsonArray) { 
     Json *json = [[Json alloc] initWithDictionary:dic]; 
     [tempJson addObject:json]; 
     } 
      self.jsons = [[NSArray alloc] initWithArray:tempJson]; 
      [self.tableView reloadData]; 
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" 
               message:[error localizedDescription] 
               delegate:nil 
             cancelButtonTitle:@"Ok" 
             otherButtonTitles:nil]; 
      [alertView show]; 
      }]; 
     [operation start]; 
     } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
     { 
     return self.jsons.count ; 

     } 

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     { 
      static NSString *Cellidentifier1 = @"ysTableViewCell"; 

      ysTableViewCell *cell1 = [tableView 

      dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath]; 
      cell1.TitleLabel1.text = [self.jsons[indexPath.row] title]; 

      cell1.AuthorLabel1.text = [self.jsons[indexPath.row] author]; 
      [cell1.ThumbImage1 setImageWithURL:[NSURL URLWithString: 

      [self.jsons[indexPath.row] a_image]]]; 
      return cell1;} 
     - (void)scrollViewDidScroll: (UIScrollView*)scroll { 

      CGFloat currentOffset = scroll.contentOffset.y; 
      CGFloat maximumOffset = scroll.contentSize.height -  scroll.frame.size.height; 

      self.tableView.contentInset = UIEdgeInsetsMake(65, 0, 0, 0); 
      if (maximumOffset - currentOffset <= -60.0) { 
      x++; 

      [self loadNinjas]; 
      [self.tableView addInfiniteScrollingWithActionHandler:^{ 
      }]; 
      [self.tableView reloadData]; 
     } 
     } 

Répondre

5

- (void)scrollViewDidScroll: (UIScrollView*)scroll est appelé une cuple fois pendant le défilement

Vous devriez mieux utiliser:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 

OU

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView 
    willDecelerate:(BOOL)decelerate 
+0

merci son travail bien. - (void) scrollViewDidEndDragging: (UIScrollView *) scrollView willDecelerate: (BOOL) décélère – Daljeet

Questions connexes