2014-09-17 1 views
0

JSON 데이터를 UITableView로 가져 오기 위해 AFNetworking 써드 파티 라이브러리를 사용하고 있습니다.scrollViewDidScroll 메서드로 UITableView에서 페이지 매김을 구현하고 있습니까?

Json 링크는 www.example.com입니다. & 페이지 = 1은 1-10 개의 기사를 제공하고 www.example.com은 & 페이지 = 2는 11-20 개의 기사를 제공합니다.

페이지 매김을 구현했으며 scrollViewDidScroll 메서드는 사용자가 스크롤 할 때 다음 10 개의 기사를 제공한다는 것을 의미합니다.

앱 실행 및 UITableView가 scrollViewDidScroll 메서드를로드 할 때 세 번 호출되지만 예상 호출을 한 번 호출하면 문제가 발생합니다.

scrollViewDidScroll 메서드에서 페이지 매김에 대한 증분 변수를 사용하고 있는데 3 번 호출하고 x 값이 3이고 30 개의 기사가 포함되어 있습니다.

사용자가 다시 스크롤하면 다음 30 개 기사가 제공됩니다. 앱이 시작될 때 scrollViewDidScroll 메서드가 세 번 호출 된 이유를 파악할 수 없습니다. , 이것은 50 개 세포 및 페이지 아래로 사용자가 스크롤로있는 tableView를 초기화하는 간단한 코드입니다

 - (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]; 
      } 
      } 

답변

0

있는 tableView에 셀에 도달 할 때마다 20 개 새로운 세포를 추가

내 코드입니다 표의 끝 부분에서 10 셀입니다.

int i; 
int lastSeen; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    i = 50; 
    lastSeen = 0; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return i; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath]; 

    cell.textLabel.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row]; 

    lastSeen = (lastSeen < indexPath.row) ? indexPath.row : lastSeen; 

    return cell; 
} 
- (void)scrollViewDidScroll: (UIScrollView*)scroll { 
    if (lastSeen >= (i - 10)) { 
     i += 20; 

     //load new data here. 

     [self.tableView reloadData]; 

    } 
} 
관련 문제