Reputation: 63
I know your automatically going to try and say this is the same as passing data through view controllers page post, well its not because in this scenario it is very different and when was UIColletionView introduced because there seems to be hardly any information on it compared to a UITableView.
So heres the question i have a UICollectionView which has 3 cells named A B and C picture as follows:
When i click on UiCollectionViewCell "A" i would like it to send me to a new blank ViewController of which implements UICollectionViewCell Label (Which Displays the letter A) to my UIViewController Title.
My Current code is as follows, i already know that i missing my -void prepare for segue code, that is what i should be using right? and i am aware that i should have UICollcetionView items selected code but that is what i am struggling to setup and for the life of me i have been searching the internet and about 300 posts on here find out how to do this, there is no record or my search query must clearly be missing something important from, i can do this all day long using a table view but have never used a comllcetionview before i would have assumed that based on previous code of a table view the code would be similar but i just can't find out how to do what i want to do.
GroupsViewController.h
#import <UIKit/UIKit.h>
@interface GroupsViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *GroupsCollectionView;
- (IBAction)cellToggleAction:(id)sender;
@end
GroupsViewController.m
#import "GroupsViewController.h"
#import "GroupsHomeViewController.h"
#import "CustomCell.h"
@interface GroupsViewController ()
{
NSArray *arrayOfImages;
NSArray *arrayOfDescriptions;
}
@end
@implementation GroupsViewController
{
NSString *reuseIdentifier;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self GroupsCollectionView]setDataSource:self];
[[self GroupsCollectionView]setDelegate:self];
reuseIdentifier= @"SmallIcon";
arrayOfImages = [[NSArray alloc]initWithObjects:@"A.png",@"B.png",@"C.png",nil];
arrayOfDescriptions = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",nil];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrayOfDescriptions count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
[[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
[[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
// Toggle View Button
- (IBAction)cellToggleAction:(id)sender {
if([reuseIdentifier isEqualToString:@"SmallIcon"]){
reuseIdentifier=@"ListView";
[sender setImage:[UIImage imageNamed:@"LargeIcon"]];
}
else if
([reuseIdentifier isEqualToString:@"ListView"]){
reuseIdentifier=@"LargeIcon";
[sender setImage:[UIImage imageNamed:@"SmallIcon"]];
}
else if
([reuseIdentifier isEqualToString:@"LargeIcon"]){
reuseIdentifier=@"SmallIcon";
[sender setImage:[UIImage imageNamed:@"ListView"]];
}
[self.GroupsCollectionView reloadData];
}
//Toggled Cell Sizes
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize cellSize;
if([reuseIdentifier isEqualToString:@"SmallIcon"])
cellSize = CGSizeMake(100, 130);
else if
([reuseIdentifier isEqualToString:@"ListView"])
cellSize = CGSizeMake(320, 50);
else if
([reuseIdentifier isEqualToString:@"LargeIcon"])
cellSize = CGSizeMake(320, 350);
return cellSize;
}
@end
GroupsHomeViewController.h
#import <UIKit/UIKit.h>
@interface GroupsHomeViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *logoImage;
@property (strong, nonatomic) IBOutlet UILabel *groupLabel;
@end
GroupsHomeViewController.m
#import "GroupsHomeViewController.h"
@interface GroupsHomeViewController ()
@end
@implementation GroupsHomeViewController
-(void)viewDidLoad{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
CustomCell.h
#import <UIKit/UIKit.h>
@interface CustomCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *IconImage;
@property (weak, nonatomic) IBOutlet UILabel *IconLabel;
@end
CustomCell.m
#import "CustomCell.h"
@implementation CustomCell
@end
If you would like any more information for you to further understand what i am trying to accomplish then please don't hesitate to comment below and thank you in advance for any answers.
Upvotes: 0
Views: 68
Reputation: 1956
You need to create a property in the View controller that you want to navigate to on selection of a Collection view cell.
So in your view controller's .h file add
@property (nonatomic, strong) NSString* titleText;
And create a custom setter for this property. This is where we will set the view controller's title.
In your view controller's .m file add
- (void)setTitleText:(NSString *)titleText {
_titleText = titleForNextVC;
// Set Title of your ViewController
self.title = _titleText;
}
Now you need to pass the string from CustomCell
in your GroupsViewController
to the next view controller.
For this implement the collectionView:didSelectItemAtIndexPath:
method in GroupsViewController.m
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
CustomCell* cell = [self collectionView:collectionView cellForItemAtIndexPath:indexPath];
_titleForNextVC = cell.IconLabel.text;
}
Here titleForNextVC
is simply an NSString
property in GroupsViewController.m class extension.
@interface GroupsViewController ()
{
NSArray *arrayOfImages;
NSArray *arrayOfDescriptions;
NSString* _titleForNextVC;
}
Now, in just pass this string to the titleText
property of your next view controller in prepareForSegue:sender:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:yourSegueIdentifier]) {
// Replace YourNextViewController with actual class of your UIViewController
YourNextViewController *vc = (YourNextViewController *)segue.destinationViewController;
vc.titleText = _titleForNextVC;
}
}
And you are done.
Upvotes: 1