简体   繁体   English

集合视图分页IOS

[英]Collection View Pagination IOS

I want to apply a pagnination on collectionView such that if items are less than equal to 4,the collectionView reloads the data. 我想在collectionView上应用pagnination,以便如果项目小于等于4,则collectionView会重新加载数据。 I tried but result is not approppriate, all cells are loaded for the first time and when scroll ended, it terminates the app, as I incremented the cell items to 1, so it terminates the app. 我尝试过,但结果不适当,所有单元格都首次加载,滚动结束时,它终止了应用程序,因为我将单元格项增加到1,所以它终止了应用程序。

How can I load only 4 cells for the first time and then after scrolling reload the remaining. 我如何第一次仅加载4个单元格,然后在滚动后重新加载其余单元格。

My ViewController Code: 我的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;

    }

When asked for the cell you are getting your data out of your array before you check if the supplied index is valid. 当要求提供该单元格时,您需要先从数组中获取数据,然后再检查提供的索引是否有效。 This line should be moved inside the if statement to be safe. 为了安全起见,应将此行移至if语句内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM