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
Related
I want to achieve an effect that can turn to red when a cell is clicked in CollectionView,so I use CAlayer,but it does not work. When I use target-action inside cell to achieve, it can work perfectly.
Here are the codes:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
videoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"videoCell" forIndexPath:indexPath];
CALayer *testLayer = [[CALayer layer] init];
testLayer.frame = cell.bounds;
testLayer.backgroundColor = [UIColor redColor].CGColor;
[cell.layer addSublayer:testLayer];
}
You need to use the cell's contentView to accomplish this. Also, remember cells are caches and so you can not use state that you store inside a cell. You have to store it somewhere else. And you should not use didSelectItemAtIndexPath to update the UI. Rather change the state there and then request and update of the relevant cell.
Here is a nice example to illustrate. In this example you can select multiple cells and they all will have a red background. The selection state of a cell is stored inside a dictionary in the controller. You could easily change this if you e.g. only want to have a single cell selected at a given moment in time. Then you need to also updated the cell that became unselected but that is another example.
#import "CollectionViewController.h"
#interface CollectionViewController () < UICollectionViewDelegate, UICollectionViewDataSource >
#property (nonatomic,strong) NSMutableDictionary * selectedCells; // Key is integer row, value is boolean selected
#end
#implementation CollectionViewController
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
// Do any additional setup after loading the view.
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
// Empty / nothing selected for now
self.selectedCells = NSMutableDictionary.dictionary;
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell based on state
if ( [( NSNumber * )[self.selectedCells objectForKey:#( indexPath.row )] boolValue] ) {
cell.contentView.backgroundColor = UIColor.redColor;
} else {
cell.contentView.backgroundColor = UIColor.blueColor;
}
return cell;
}
#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// Flip selected state
[self.selectedCells setObject:#( ! [( NSNumber * )[self.selectedCells objectForKey:#( indexPath.row )] boolValue] )
forKey:#( indexPath.row )];
// Request an UI update to reflect the updated state
[collectionView reloadItemsAtIndexPaths:#[ indexPath ]];
}
#end
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);
}
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]];
}
}
I am using UICollectionView and I want to add UISearchBar or UISearchDisplayController to it.
Can i do that. I want some example please.
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
CollectionReusable *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeadCell" forIndexPath:indexPath];
reusableview = headerView;
}
if (kind == UICollectionElementKindSectionFooter) {
CollectionReusable *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:#"FootCell" forIndexPath:indexPath];
reusableview = footerView;
}
return reusableview;
}
It's a bit late but this repository contains a great piece of code dealing with this issue.
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...
}