uicollection view datail view - cocoa-touch

I am new in iphone. I have a collection view in my app and it work fine but now I want to select an image and that image I want in next viewcontroller in another imageview. - (NSInteger)collectionView:
I use the following code:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return recipeImages.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
return cell;
}

Implement collectionView:didSelectItemAtIndexPath::
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.item]];
// go on...
}

Related

How to display only one cell of the collection view on the screen anytime in objective-c

I have a collectionview with custom collectionviewcell. I want to make a gallery that only one image display on the screen at any time. What i have to do ?
Here is my code :
[myCollectionView registerClass:[myCustomCollectionViewCell class] forCellWithReuseIdentifier:"myIdentifier"];
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
myCustomCollectionViewCell *cell = (myCustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"myIdentifier" forIndexPath:indexPath];
cell.layer.masksToBounds=NO;
cell.imageView.image = [UIImage imageNamed:#"image"];
return cell;
}
First, register your collection view class like below
[yourCollectionView registerClass:[yourCustomCollectionViewCell class] forCellWithReuseIdentifier:"yourIdentifier"];
Second, implement collection view datasource method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
yourCustomCollectionViewCell *cell = (yourCustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"yourIdentifier" forIndexPath:indexPath];
cell.layer.masksToBounds=NO;
cell.imageView.image = [UIImage imageNamed:#"yourImageName"];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// update size accordingly
return CGSizeMake(100,100);
}

To change color only tapped image color of UICollectionView in XCode

I've created image in NSArray as follow in viewDidLoad.
menuImgs = #[#"image1", #"image2", #"image3", #"image4"];
And apply above images in UICollectionView as follow.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"HomeCell" forIndexPath:indexPath];
UIImage *iconImg = [UIImage imageNamed:menuImgs[arrayCountChecker]];
UIImageView *cellImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:menuImgs[arrayCountChecker]]];
cellImage.frame = CGRectMake(cellWidth/2-iconImg.size.width,cellHeight/2-iconImg.size.height, iconImg.size.width, iconImg.size.height);
[cell addSubview:cellImage];
return cell;
}
What I want is I want to change image color when tapped on specific UICollectionViewCell in this function.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
If you just want to change background color of selected cell then use cell.selectedBackgroundView otherwise,
intialize one array and store selected index in that array. And when collectionview load data check index from array and change image color according. When someone select new cell remove old index from array and store new and and reload colloectionview. Here is code example.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
cell.lblFilter.text = filterTitles[indexPath.row];
cell.imgvFilter.image = filterImages[indexPath.row];
if([indexPath isEqual:selectedIndexPath])
{
cell.imgvFilter.superview.layer.borderColor = [UIColor lightBlue].CGColor;
cell.lblFilter.textColor = [UIColor lightBlue];
}
else
{
cell.lblFilter.textColor = [UIColor textGray];
cell.imgvFilter.superview.layer.borderColor = [UIColor borderGray].CGColor;
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collvFilter scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
if (![indexPath isEqual:selectedIndexPath])
{
NSIndexPath *indexPathToReload = selectedIndexPath;
selectedIndexPath = indexPath;
[collvFilter reloadItemsAtIndexPaths:#[indexPathToReload, indexPath]];
}
}

UIView in UICollectiveViewCell can't hidden

I got a UICollectionView that I have created programmatically. I would like to have a collection view to behave in the following direction:
User touches cell
hidden uiview that cover image
User touches cell again
show uiview to cover the image
1.) Before user touch image in cell it look like this
http://upload.siamza.com/1811355
2.) After user touch image in cell it will look like this
http://upload.siamza.com/1811359
3.) If user touch image again it 'll look like 1.)
right now my select and deselect is look like this:
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
if(collectionView == genrescollectView){
genresRecCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"genresRecCell" forIndexPath:indexPath];
cell.checkforselectView.hidden = FALSE;
}
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if(collectionView == genrescollectView){
genresRecCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"genresRecCell" forIndexPath:indexPath];
cell.checkforselectView.hidden = TRUE;
}
}
but it seen to be not working at
cell.checkforselectView.hidden = FALSE;
and
cell.checkforselectView.hidden = TRUE;
I have check in cellForItemAtIndexPath for work or not by adding
cell.checkforselectView.hidden = FALSE;
and it work so I wonder anyone could help me with this problem and this is now in cellForItemAtIndexPath
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
genresRecCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"genresRecCell" forIndexPath:indexPath];
NSString *imageGenres = [genresData objectAtIndex:indexPath.row];
cell.genresImage.image = [UIImage imageNamed:imageGenres];
NSString *textToParse = imageGenres;
NSArray *components = [textToParse componentsSeparatedByString:#"."];
NSString *genresText = [components firstObject];
cell.labelGenres.text = genresText;
cell.genresImage.layer.cornerRadius = 10.0f;
cell.genresImage.layer.masksToBounds = YES;
//---------> this is where I check whether it work or not(and it work).
cell.checkforselectView.hidden = FALSE;
return cell;
}
Thank in advance :)
in
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
use
[collectionView cellForItemAtIndexPath:indexPath]
instead of dequeing a new cell

How to link a UICollectionViewCell to a UICollectionView

I have a UIViewController with a UICollectionView on it but Xcode doesn't seem to look like every tutorial I find on the Internet or on YouTube - When I drag a UICollectionViewCell to place in the UICollectionView, it won't let me place it.
Now I'm confused as to how I can link my cell to the UICollectionView.
This is the viewController.h file:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.imagesArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"imageCell" forIndexPath:indexPath];
NSString *myImageString = [self.imagesArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:myImageString];
return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(100.0, 100.0);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(5, 5, 5, 5);
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
//[self.collectionView registerClass:[ImageViewCell class] forCellWithReuseIdentifier:#"imageCell"];
self.imagesArray = #[#"shirt1.PNG", #"pants.png", #"pants2.png"];
}
I'm not using a Storyboard interface but individual xib's. When I run this all that appears is the blank black screen. What am I missing?
-registerNib:forCellWithReuseIdentifier:
If you have your cell defined in a NIB, then you register that NIB with the collection view. That is how the collection view know what to load when -dequeueReusableCellWithReuseIdentifier:forIndexPath: is called.
- (void)viewDidLoad
{
// …
UINib nib = [UINib nibWithNibName:#"<the name of your xib>" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:#"imageCell"];
// …
}
Because you didn't have new a object of UICollectionViewCell in this method:
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
The "cell" in your method must be set to nil.

multiple selection in uicollectionview not working in ios

i have multiple image in uicollectionview like grid view. i want select multiple image at a time but not working this code.please any one give idea about this code.
i have already tried this code but not working.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
NSMutableArray *indexPaths = [NSMutableArray arrayWithObject:indexPath];
if (self.selectedItemIndexPath)
{
// if we had a previously selected cell
if ([indexPath compare:self.selectedItemIndexPath] == NSOrderedSame)
{
// if it's the same as the one we just tapped on, then we're unselecting it
self.selectedItemIndexPath = nil;
}
else
{
// if it's different, then add that old one to our list of cells to reload, and
// save the currently selected indexPath
[indexPaths addObject:self.selectedItemIndexPath];
self.selectedItemIndexPath = indexPath;
}
}
else
{
// else, we didn't have previously selected cell, so we only need to save this indexPath for future reference
self.selectedItemIndexPath = indexPath;
}
// and now only reload only the cells that need updating
[self.collectionView reloadItemsAtIndexPaths:indexPaths];
}
// I Have Drag the UICollectionView Controller in storyboard
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
arrImage = [[NSMutableArray alloc]initWithObjects:#"1.jpeg",#"2.jpeg",#"3.jpeg",#"4.jpeg",#"5.jpeg",#"6.jpeg",#"7.jpeg",#"8.jpeg",#"9.jpeg",#"10.jpeg",#"flower.jpeg",#"flower1.jpeg", nil];
[self.collectionView setAllowsMultipleSelection:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrImage count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]];
[self.view addSubview:recipeImageView];
cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]]];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.15];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]];
}