colection view exam
#import "CollectionViewController.h"
#import "BuyViewController.h"
#import "CollectionViewCell.h"
@interface CollectionViewController ()
{
NSArray *mobiles;
NSArray *costumes;
NSArray *shoes;
NSInteger selectpath;
NSArray *mobilerate;
NSArray *costumerate;
NSArray *shoerate;
}
@end
@implementation CollectionViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = self.receivename;
mobiles = [[NSArray alloc]initWithObjects:@"7.jpg",@"6.jpg",@"5.jpg", nil];
costumes = [[NSArray alloc]initWithObjects:@"shirt.jpg",@"costume2.jpg",@"costume1.jpg", nil];
shoes = [[NSArray alloc]initWithObjects:@"shoe.jpg",@"shoe1.jpg",@"shoe2.jpg", nil];
mobilerate = [[NSArray alloc]initWithObjects:@"10000",@"11000",@"13000",nil];
costumerate = [[NSArray alloc]initWithObjects:@"699",@"999",@"899", nil];
shoerate = [[NSArray alloc]initWithObjects:@"599",@"499",@"300", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 3;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
UIImageView *collectionImg = (UIImageView *)[cell viewWithTag:100];
if ([self.receivename isEqualToString:@"Mobiles"])
{
collectionImg.image = [UIImage imageNamed:[mobiles objectAtIndex:indexPath.row]];
}
else if ([self.receivename isEqualToString:@"Costumes"])
{
collectionImg.image = [UIImage imageNamed:[costumes objectAtIndex:indexPath.row]];
}
else
{
collectionImg.image = [UIImage imageNamed:[shoes objectAtIndex:indexPath.row]];
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
selectpath = indexPath.row;
[self performSegueWithIdentifier:@"buynow" sender:self];
}
// In a storyboard-based application, you will often want to do a little
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"buynow"])
{
BuyViewController *obj = segue.destinationViewController;
if ([self.receivename isEqualToString:@"Mobiles"])
{
obj.reciveimg = [mobiles objectAtIndex:selectpath];
obj.labelrecive = [mobilerate objectAtIndex:selectpath];
}
else if ([self.receivename isEqualToString:@"Costumes"])
{
obj.reciveimg = [costumes objectAtIndex:selectpath];
obj.labelrecive = [costumerate objectAtIndex:selectpath];
}
else
{
obj.reciveimg = [shoes objectAtIndex:selectpath];
obj.labelrecive = [shoerate objectAtIndex:selectpath];
}
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
}
@end
.h file
@interface CollectionViewController :
UIViewController<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong,nonatomic) NSString *receiveimg;
@property (strong,nonatomic) NSString *receivecostume;
@property (strong,nonatomic)NSString *receivename;
@end