2014-12-28 4 views
0

아이템이 4보다 작 으면 콜렉션 뷰가 데이터를 다시로드하도록 콜렉션 뷰에 몰래 적용을 적용하고 싶습니다. 시도했지만 결과가 적합하지 않습니다. 모든 셀이 처음으로로드되고 스크롤이 끝나면 셀 항목을 1 씩 증가시켜 앱을 종료하므로 앱이 종료됩니다.콜렉션 뷰 페이지 매김보기 IOS

어떻게 처음으로 4 개의 셀만로드 한 다음 스크롤하여 나머지를 다시로드 할 수 있습니까?

내의 ViewController 코드 :

NSInteger _currentPage; 
@property (nonatomic, retain) NSMutableArray *items; 

MY CODE : 지정된 인덱스 여부를 확인하기 전에 당신이 당신의 배열에서 데이터를 얻고있는 셀을 요청

#define ITEMS_PAGE_SIZE 4 
#define ITEM_CELL_IDENTIFIER @"Cell" 
#define LOADING_CELL_IDENTIFIER @"LoadingItemCell" 
- (void)viewWillAppear:(BOOL) animated 

{ 
UICollectionViewFlowLayout *collectionViewLayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout; 
    collectionViewLayout.sectionInset = UIEdgeInsetsMake(20, 0, 20, 0); 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return arr.count+1; 
} 

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
     Data* d = arr[indexPath.row]; 
      if (indexPath.item < arr.count) { 
       UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ITEM_CELL_IDENTIFIER forIndexPath:indexPath]; 

       UILabel *label = (UILabel *)[cell viewWithTag:200]; 
       label.text = [NSString stringWithFormat:@"%@",d.detail]; 
       return cell; 
     } else { 
      NSLog(@"FETCHING MORE ITEMS ******************"); 

      // Generate the 'next page' of data. 
      NSMutableArray *newData = [NSMutableArray array]; 
      NSInteger pageSize = ITEMS_PAGE_SIZE; 
      for (int i = _currentPage * pageSize; i < ((_currentPage * pageSize) + pageSize); i++) { 
       [newData addObject:[NSString stringWithFormat:@"Item #%d", i]]; 
      } 

      _currentPage++; 


      // Simulate an async load... 

      double delayInSeconds = 3; 
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 

       // Add the new data to our local collection of data. 
       for (int i = 0; i < newData.count; i++) { 
        [self.items addObject:newData[i]]; 
       } 

       // Tell the collectionView to reload. 
       [self.collectionView reloadData]; 

      }); 
     } 
     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LOADING_CELL_IDENTIFIER forIndexPath:indexPath]; 

     UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] 
                 initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
     activityIndicator.center = cell.center; 
     [cell addSubview:activityIndicator]; 

     [activityIndicator startAnimating]; 

     return cell; 

    } 

답변

0

유효합니다. 이 행은 if 문 내에서 안전하게 이동해야합니다.

관련 문제