I am trying to create a simple collection view with custom cells that have an UIImageView as a background. It's going to look something like :
This is my code :
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:#"MY_CELL"];
leftInset = rightInset = 0;
numberOfDays = 31;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 200;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
return numberOfDays;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"MY_CELL" forIndexPath:indexPath];
if(indexPath.row < 10)
cell.imageView.image = [UIImage imageNamed:#"no"];
else if(indexPath.section%3==1)
{
cell.imageView.image = [UIImage imageNamed:#"ok"];
}
else if(indexPath.section%3==2)
{
cell.imageView.image = [UIImage imageNamed:#"sa"];
}
else{
cell.imageView.image = [UIImage imageNamed:#"happy_vote"];
}
return cell;
}
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(CELL_DIM, CELL_DIM);
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// top left bottom right
return UIEdgeInsetsMake(5, leftInset, 0, 0);
}
MyCell.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(#"Init with frame!");
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.height)];
[self.contentView addSubview:self.imageView];
[self.imageView setBackgroundColor:[UIColor redColor]];
}
return self;
}
It works fine for around 10 sections in the collection view. The problem is when I want to add more sections, the vertical scrolling starts to move really slow. I noticed that all the cells are allocated all at once. I am searching for a way to make this more resource friendly, and have the scrolling move continuously rather than slow and laggish. Could you tell me how to take care of the resources allocation to avoid this problem ? Thank you.
PS : I've tried removing the image view from the cell background and in the method cellForItemAtIndexPath: set a background color for the cell but again, after 10 or more sections it moves really slow.
What you might want to do to help performance time is to check if an object is already initiated. You could also separately instantiate your images so that it doesnt have to be read from memory every time:
Make image1 a property of the view controller:
- (void)viewDidLoad
{
image1 = [UIImage imageNamed:#"image1"];
image2 = [UIImage imageNamed:#"image2"];
image3 = [UIImage imageNamed:#"image3"];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"MY_CELL" forIndexPath:indexPath];
if(indexPath.row < 10)
cell.imageView.image = [UIImage imageNamed:#"no"];
else if(indexPath.section%3==1)
{
cell.imageView.image = image1;
}
else if(indexPath.section%3==2)
{
cell.imageView.image = image2;
}
else{
cell.imageView.image = image3;
}
return cell;
}
Related
I have an UICollection View i am using the peek and pop method to look into it normally it works fine but if i use peek and pop i get the error
The code for the collection view class
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
AppsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
cell.titleLabel.text = #"TwitClient";
cell.iconimg.image = [UIImage imageNamed:#"twitclient"];
cell.appsbutton.userInteractionEnabled = NO;
cell.appsbutton.hidden = NO;
cell.appsbutton.alpha = 0.0;
cell.phoneImageView.image = [UIImage imageNamed:#"app3"];
cell.descLabel.text = #"TwitClient is an app which helps you find tweets on any subject from around the earth. You can search tweets with or without hashtags";
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(69, 126, 277, 490)];
imageView.image = [UIImage imageNamed:#"T-2"];
[cell addSubview:imageView];
return cell;
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
This is where 3d touch is implmented
- (UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
AppsViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"...."];
previewingContext.sourceRect = self.appsView.frame;
return appsvc;
}
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self presentViewController:viewControllerToCommit animated:YES completion:nil];
}
I dont really know what i have done wrong i have done another collection View like this but no error's
I am using third party UiCollection View KRLCollectionViewGridLayout.
In this I have the following issue attached in screen shot.
See the above screen shot. There is an extra space in collectionview cell.
My Question is how to remove these extra space in collection cell and also how to add separator between two cell?
Here is my Code:
- (KRLCollectionViewGridLayout *)layout
{
return (id)self.collectionView.collectionViewLayout;
}
- (IBAction)changeColumnsTapped:(id)sender {
[[[UIActionSheet alloc] initWithTitle:#"Choose how many columns"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:#"1",#"2", nil]
showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
self.layout.numberOfItemsPerLine = [[actionSheet buttonTitleAtIndex:buttonIndex] integerValue];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, 44.0)];
[self.collectionView addSubview:searchBar];
[self.collectionView setContentInset:UIEdgeInsetsMake(44, 0, 0, 0)];
self.layout.numberOfItemsPerLine = 1;
self.layout.aspectRatio = 1;
self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
self.layout.interitemSpacing = 10;
self.layout.lineSpacing = 10;
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
//recipePhotos = [NSMutableArray arrayWithObjects:#"image1.png",#"image2.png",#"image3.png", nil];
recipePhotos = [NSMutableArray arrayWithObjects:#"image1.png", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [recipePhotos count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell1" forIndexPath:indexPath];
//cell.contentView.backgroundColor = [UIColor blueColor];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];
cell.layer.borderWidth = 0.5;
return cell;
}
I'm the author of this layout. As it stands now, it looks like you're using the default value of the layout's aspectRatio property of 1/1 which is what is in charge of sizing the cells. Unfortunately this layout does not yet support dynamically-sized cells, but I may do so in the future. If you know how high your cells are intended to be relative to their width, you can set the aspect ratio accordingly. an aspect ratio of 2 would mean it's twice as long as it is tall, and an aspect ratio of 0.5 would mean it's 1/2 as long as it is tall. You probably want some number greater than 1 for your ratio to decrease the spacing.
If that doesn't give you the effect you want, you may need to switch to UICollectionViewFlowLayout instead which gives you precise control over the size of cells.
Try This. Check This Link Cell spacing in UICollectionView
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 2.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 2.0;
}
// Layout: Set Edges
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right
return UIEdgeInsetsMake(0,0,0,0); // top, left, bottom, right
}
Add UICollectionViewDelegateFlowLayout delegate in you interface
and ipmplement
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return IPAD ? CGSizeMake(75, 75) : CGSizeMake(55, 55);
}
And change size as per your requirement.
I have maked following view.
But when calling -reloadData, the view becomes...
I think it's because cells is reused.
Do you have any idea to keep view as first image even if calling reloadData?
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
if (indexPath.item == 0)
{
cell.label.text = #"Hello";
}
else
{
UIImage *image = dataArray[indexPath.item];
cell.imageView.image = image;
}
return cell;
}
CustomCell.m
#property (nonatomic, strong) UIImageView* imageView;
#property (nonatomic, strong) UILabel* label;
//...
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.imageView = [[UIImageView alloc]initWithFrame:imgViewFrame];
[self.contentView addSubview:self.imageView];
self.label = [[UILabel alloc]initWithFrame:labelFrame];
[self.contentView addSubview:self.label];
}
return self;
}
EDIT:
I modified my code.
- setting all default values before setting them with correct data
- calling -prepareForReuse
When calling -reloadData, Although imageView.image stay nil, label.text in indexPath.item==0 becomes nil and label.text in indexPath.item==3 outputs #"Hello".
Yes, its because of reusability. You can avoid it by changing line of code to this:
if (indexPath.item == 0)
{
cell.label.text = #"Hello";
cell.imageView.image = nil;
}
else
{
UIImage *image = dataArray[indexPath.item];
cell.imageView.image = image;
cell.label.text = #"";
}
When you're retrieving the dequeued cells they are already setup. So the first time you retrieve 9 different cells and initialise them accordingly.
The second time tough the cells properties are already set so you need to restart them.
So your code should look as follows:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
if (indexPath.row == 0)
{
cell.imageView.image = nil;
cell.label.text = #"Hello";
}
else
{
UIImage *image = dataArray[indexPath.item];
cell.imageView.image = image;
cell.label.text = nil;
}
return cell;
}
Try setting all default values of your cell before doing any check:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
//Set all default values before setting them with correct data
cell.imageView.image = nil;
cell.label.text = #"";
if (indexPath.row == 0)
{
cell.label.text = #"Hello";
}
else
{
UIImage *image = dataArray[indexPath.item];
cell.imageView.image = image;
}
return cell;
}
EDIT:
You could add this method to your CustomCell.m
- (void)prepareForReuse
{
//Set default values here
}
REPORT:
I solved the issue on my own.so, I will report it.
I used the property hidden.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
if (indexPath.item == 0)
{
cell.label.text = #"Hello";
cell.imageView.hidden = YES;
}
else
{
UIImage *image = dataArray[indexPath.item];
cell.imageView.image = image;
cell.label.hidden = YES;
}
return cell;
}
I'm trying to create a week calendar by using uiclloectionview. here is what it would look like when the view come to focus:
original view
when I scroll to bottom and scroll back to top. those cells that were disappeared because of scrolling change text.
scrolled view
Here is my code:
#interface ViewController ()
#end
#implementation ViewController
#synthesize calendar;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self.calendar setCollectionViewLayout:[[CalendarFlowLayout alloc]init]];
timeline = [NSArray arrayWithObjects:#"8:00", #"9:00",#"10:00",#"11:00",#"12:00",#"13:00",#"14:00",#"15:00",#"16:00",#"17:00",#"18:00",#"19:00",#"20:00",#"21:00",#"22:00",nil];
weekline=[NSArray arrayWithObjects:#"Mon", #"Tue",#"Wen",#"Thr",#"Fri",#"Sat",#"Sun",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 128;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
if (![cell.contentView viewWithTag:2]) {
UILabel * title;
UIView *message;
title =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
title.font=[UIFont systemFontOfSize:14.0];
title.textAlignment=NSTextAlignmentCenter;
//title.backgroundColor=[UIColor redColor];
if (indexPath.row%8==0) {
int time =indexPath.row/8;
if (time<[timeline count]) {
title.text =[NSString stringWithFormat:#"%#",[timeline objectAtIndex:time]];
}
}else if(indexPath.row>0&&indexPath.row<8){
if (indexPath.row<[weekline count]+1) {
title.text =[NSString stringWithFormat:#"%#",[weekline objectAtIndex:indexPath.row-1]];
}
[cell.layer setBorderColor:[UIColor blackColor].CGColor];
[cell.layer setBorderWidth:1.0f];
}else{
[cell.layer setBorderColor:[UIColor blackColor].CGColor];
[cell.layer setBorderWidth:1.0f];
}
title.textColor=[UIColor blackColor];
message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
message.tag = 2;
message.backgroundColor =[UIColor clearColor];
[message addSubview:title];
[cell.contentView addSubview:message];
}
return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGFloat width =[[UIScreen mainScreen]bounds].size.width/8;
return CGSizeMake(width, width);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 0.0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 0.0;
}
#end
I just wonder why indexpath keep changing and how to fix this issue? thank you.
You only configure the cell if it doesn't have the message UIView (tag 2) - so when cells are reused you are simply returning them as they were configured previously - however there is no guarantee that the cell be reused in the same indexPath as it was previously - in fact you can pretty much guarantee that it won't.
You need to correctly configure your cell every time. This will be much easier if you configure your prototype cell in the storyboard, or if you don't want to do that create a UICollectionViewCell subclass that adds the title and message controls in its init method.
Hi i have created one UIViewController in that i added one UICollectionView with CustomCell. Then, i created a uicollectionviewcell class for cell. Finally set the delegate and datasource in storybord itself. But it showing empty page. like in below image
CODE:
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionview = _collectionview;
self.collectionview.dataSource = self;
self.collectionview.delegate = self;
speakersImages=[[NSMutableArray alloc]init];
// Do any additional setup after loading the view, typically from a nib.
imageLabels=[[NSMutableArray alloc]initWithObjects:#"RB Forum 2013",#"Agenda",#"Speakers",#"Contact",#"Map",#"Websites",#"#Responsible Biz",#"Partners",#"Sustainability",nil];
imagePaths=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"RBform.png"],[UIImage imageNamed:#"agenda.png"],[UIImage imageNamed:#"speakers.png"],[UIImage imageNamed:#"contact.png"],[UIImage imageNamed:#"map.png"],[UIImage imageNamed:#"website.png"],[UIImage imageNamed:#"twitter.png"],[UIImage imageNamed:#"partners.png"],[UIImage imageNamed:#"sustainability.png"], nil];
[self.collectionview registerClass:[NewCell class] forCellWithReuseIdentifier:#"Cell"];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return [imageLabels count];
}
//THE BELOW DELEGATE METHOD DOES NOT GET CALLED!
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// we're going to use a custom UICollectionViewCell, which will hold an image and its label
//
static NSString *CellIdentifier = #"Cell";
NewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
// make the cell's title the actual NSIndexPath value
cell.labels.text = [NSString stringWithFormat:#"%#",[imageLabels objectAtIndex:indexPath.row]];
// load the image for this cell
NSLog(#"%#",imageLabels);
//NSString *imageToLoad = [NSString stringWithFormat:#"%d.JPG", indexPath.row];
// cell.image.image = [UIImage imageNamed:imageToLoad];
cell.images.image = [imagePaths objectAtIndex:indexPath.row];
return cell;
}
Solution is remove
[self.collectionview registerClass:[NewCell class] forCellWithReuseIdentifier:#"Cell"];
in storybord we dont need this
Code is ok but order is different.Assigning datasource and delegates before array is populated makes the number of cell returned to 0 ,since array is not yet populated
Try like this
speakersImages=[[NSMutableArray alloc]init];
// Do any additional setup after loading the view, typically from a nib.
imageLabels=[[NSMutableArray alloc]initWithObjects:#"RB Forum 2013",#"Agenda",#"Speakers",#"Contact",#"Map",#"Websites",#"#Responsible Biz",#"Partners",#"Sustainability",nil];
imagePaths=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"RBform.png"],[UIImage imageNamed:#"agenda.png"],[UIImage imageNamed:#"speakers.png"],[UIImage imageNamed:#"contact.png"],[UIImage imageNamed:#"map.png"],[UIImage imageNamed:#"website.png"],[UIImage imageNamed:#"twitter.png"],[UIImage imageNamed:#"partners.png"],[UIImage imageNamed:#"sustainability.png"], nil];
[self.collectionview registerClass:[NewCell class] forCellWithReuseIdentifier:#"Cell"];
[self.collectionview reloadData];
this line is not needed
self.collectionview = _collectionview;
self.collectionview.dataSource = self;
self.collectionview.delegate = self;
also make sure your collection view is connected via outlet
make sure the <UICollectioniewDatasource,UICollectionviewDelegate> is there in the class header
In Your storyboard give your imageview a tag e.g. 1, and your label another tag e.g. 2
Give your cell reuse identifier that you have already gave eg. "Cell"
Now in your storyboard right click on collection view and create datasource and delegate to your viewController or file owner. alternatively you can use <UICollectionViewDataSource,UICollectionViewDelegate> in your viewController and set the delegate and datasource to self.
Now in your viewDidLoad
-(void) viewDidLoad {
self.collectionview.dataSource = self;
self.collectionview.delegate = self;
speakersImages=[[NSMutableArray alloc]init];
// Do any additional setup after loading the view, typically from a nib.
imageLabels=[[NSMutableArray alloc]initWithObjects:#"RB Forum 2013",#"Agenda",#"Speakers",#"Contact",#"Map",#"Websites",#"#Responsible Biz",#"Partners",#"Sustainability",nil];
imagePaths=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"RBform.png"],[UIImage imageNamed:#"agenda.png"],[UIImage imageNamed:#"speakers.png"],[UIImage imageNamed:#"contact.png"],[UIImage imageNamed:#"map.png"],[UIImage imageNamed:#"website.png"],[UIImage imageNamed:#"twitter.png"],[UIImage imageNamed:#"partners.png"],[UIImage imageNamed:#"sustainability.png"], nil];
}
number of items in collection view
(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return [imageLabels count];
}
create cell and reuse
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell"];
// make the cell's title the actual NSIndexPath value
UIlabel *label = (UIlabel *) [cell viewWithTag:2]
label.text = [NSString stringWithFormat:#"%#",[imageLabels objectAtIndex:indexPath.row]];
// load the image for this cell
UIImageView *imageView = (UIImageView *) [cell viewWithTag:1]
imageView.image = [imagePaths objectAtIndex:indexPath.row];
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerNib:[UINib nibWithNibName:#"NewCell" bundle:nil] forCellWithReuseIdentifier:#"CELL"];
self.collectionview.dataSource = self;
self.collectionview.delegate = self;
self.imageLabels = [NSArray arrayWithObjects:#"RB Forum 2013",#"Agenda",#"Speakers",#"Contact",#"Map",#"Websites",#"#Responsible Biz",#"Partners",#"Sustainability",nil];
self.imagePaths=[NSArray arrayWithObjects::#"RBform.png",#"agenda.png",#"speakers.png",#"contact.png",#"map.png",#"website.png",#"twitter.png",#"partners.png",#"sustainability.png", nil];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return [self.imageLabels count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CELL" forIndexPath:indexPath];
cell.labels.text = [self.imageLabels objectAtIndex:indexPath.row]];
cell.images.image = [UIImage imageNamed:[self.imagePaths objectAtIndex:index path.row]];
return cell;
}