Thursday, 4 August 2016

A Simple Collection View Example in iOS using Objective C.

Today we will learn how to create a simple collection View example in iOS.

Steps:
1. Create a new Xcode Project Lets Say(CollectionViewDemo).
2. Now go to File-New-File and create a new CocoaTouchClass. and give it name MyCollectionViewCell.


3. Now go tho storyBoard and drag and drop a collection view from object Library.
Make IBOutlet connection for collectionView. Add one image in your project. I have
added ball.png in my project.

4. Now change class of cell to MyCollectionViewCell.


5.  Now put a imageView inside the cell.


5. Now give the MyCollectionViewCell identifier name as "Cell".


6. Now make the connection for the cell imageView.

7.Now go to your ViewController.m and write the following code.

#import "ViewController.h"
#import "MyCollectionViewCell.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - collectionView Delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 12;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.cellImagView.image = [UIImage imageNamed:@"Ball.png"];
    
    return cell;
}

 8.  Now run the code and see the desired output.


You can download the whole Project from here.
Please share Your reviews.
Enjoy Coding :)


No comments:

Post a Comment

Ads Inside Post

Blogger Widget