2

Hi I am very new for Ios and in my project I am using UICollectionView ok that's fine.

But here my main requirement is that I want to load the UICollectionView cells dynamically when we are scrolling the collectionView.

I mean when we launch the app then first "5" records need to load and when we are scrolling for the first time next "5" records need to load and when we scrolling for the second time the next "5" records need to be displayed and in that way all records need to load.

Please help me.

How can I do this?

my code:-

#import "ViewController.h"

@interface ViewController ()
{
    UICollectionView * _collectionView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) collectionViewLayout:layout];

    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [_collectionView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:_collectionView];


    UIView *refreshView = [[UIView alloc] initWithFrame:CGRectMake(0, _collectionView.frame.size.height - 50, 0, 0)];
    [_collectionView addSubview:refreshView];

    UIRefreshControl *refreshControl = [UIRefreshControl new];
     refreshControl.tintColor = [UIColor redColor];
   [refreshControl addTarget:self action:@selector(viewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];
    [refreshView addSubview:refreshControl];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    for (id subview in cell.contentView.subviews) {
        if ([subview isKindOfClass:[UIImageView class]]) {
            [subview removeFromSuperview];
        } else if ([subview isKindOfClass:[UILabel class]]) {
            [subview removeFromSuperview];
        }
    }

    cell.backgroundColor = [UIColor lightGrayColor];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(70, 70);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    UIEdgeInsets insets=UIEdgeInsetsMake(10, 10, 10, 10);
    return insets;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {

    return 30.0;
}
Elydasian
  • 2,016
  • 5
  • 23
  • 41
Krish
  • 4,166
  • 11
  • 58
  • 110
  • i am not understand how to do this requirement because i am very new for this technology please help me – Krish Dec 28 '15 at 04:53
  • [This](http://stackoverflow.com/questions/15572462/how-to-insert-cell-in-uicollectionview-programatically) might help you along with Bhumica's Answer. – Bista Dec 28 '15 at 05:30

2 Answers2

0

Solution 1: If you want to do infinite scrolling in a manner more like a desktop web app, you should implement the UIScrollViewDelegate protocol (or UICollectionViewDelegate, which is a superset) and listen for the scrollViewDidScroll: callback.

Within that callback, iterate through the UICollectionView's indexPathsForVisibleItems and check to see if any of the index paths map to the 'last' item in your collection, which indicates the user has scrolled to the end.

At that point, call your logic to load more stuff.

Solution 2:

In table view, you should load more data in willDisplayCell() method. With collections, there is no such a counterpart. But, you can do like this. Hope, it helps.

(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath 
{
// Other code here ...

    if (indexPath.item == [self.data count] - 1) {
        [self loadMoreData];
    }
}

May be it will help you.

Lapinou
  • 1,467
  • 2
  • 20
  • 39
0

Solution: You can add infinite scrolling to your existing UICollectionView use the following open source library to archive your requirement.

UIScrollView-InfiniteScroll

SahilS
  • 396
  • 5
  • 7