Get position of imageView on cell of a UICollectionView - objective-c

I have a UICollectionView with custom cell that has some labels and an Image, I need to get where the image is situated in order to place a mask on top of it, I tried with the following code but every mask gets applied on top of the first cell.
This is the code I used:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UIView *darken = [[UIView alloc]init];
darken.frame = cell.img.frame;
darken.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
[collectionView insertSubview:darken aboveSubview:cell.img];
return cell;
}
How can I get the frame position of every single image on the cells?

Change your code to create just one view.
#define IMAGE_TAG 9999
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (![cell viewWithTag:IMAGE_TAG])
{
UIView *darken = [[UIView alloc]init];
darken.frame = cell.img.frame;
[darken setTag:IMAGE_TAG];
darken.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
[cell insertSubview:darken aboveSubview:cell.img];
}
return cell;
}

Related

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

UICollectionViewCell: Add a view in cell on tap

I have a uicollectionview and I am trying to add a a view to the collectionview cell when it is tapped.
Here is the code I have tried to implement
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
WeekCell *cell = (WeekCell*)[collectionView cellForItemAtIndexPath:indexPath];
NSLog(#"%#", cell.descLabel.text);
UIView *view = [[UIView alloc]initWithFrame:cell.backgroundView.bounds];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
label.text = #"new label";
[view addSubview:labels];
view.backgroundColor = [UIColor whiteColor];
[cell.backgroundView addSubview:view];
}
Here WeekCell is a custom UICollectionViewCell with a property view , backgroundview. You will also notice an NSLog in the code. This verifies that the correct cell is being retrieved. What is not working is according to the code, the text should change to white with a new UILabel but this is not the case. The cell's appearance does not change.
EDIT
As suggested I have tried to directly modify the model and call the reloadItemsAtIndexPaths to reload the data. I get an issue where the the "tapped behaviour" is being copied on to untapped cells.
Here is the new code:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if([[modelArray objectAtIndex:indexPath.row] isEqualToString:#"1"]){
cell.overviewView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imageString]];
}else{
cell.overviewView.alpha = 0;
}
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
WeekCell *cell = (WeekCell*)[collectionView cellForItemAtIndexPath:indexPath];
NSLog(#"%#", cell.descLabel.text);
if([[modelArray objectAtIndex:indexPath.row] isEqualToString:#"1"]){
[modelArray setObject:#"0" atIndexedSubscript:indexPath.row];
}else{
[modelArray setObject:#"1" atIndexedSubscript:indexPath.row];
}
NSLog(#"sequence : %#", modelArray);
[collectionView reloadItemsAtIndexPaths:#[indexPath]];
}
What I am doing is changing the alpha value of the view in the tapped cell to 0. This causes the other cells at random order to disappear once I scroll.
Instead of:
[cell.backgroundView addSubview:view];
... add to content view:
[cell.contentView addSubview:view];
Remarks:
Direct manipulation on collection view cell can cause unexpected results. For instance if cell gets reused. Better to update the model that gets rendered by the cell and call reloadItemsAtIndexPaths: that will automatically(internally) call collectionView:cellForItemAtIndexPath: which should configure(or invoke configuration routine) for the cell that can adjust its presentation.
UPDATE:
You should reset alpha of cells that should be visible, bellow is collectionView:cellForItemAtIndexPath: method with correction:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if([[modelArray objectAtIndex:indexPath.row] isEqualToString:#"1"]){
cell.overviewView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imageString]];
cell.overviewView.alpha = 1;
} else {
cell.overviewView.alpha = 0;
}
}
The backgroundView property of a UICollectionViewCell is placed behind the content view. So the reason for the label not being visible could be because it is masked by the content view.
You could set clearColor for the Content view views or add the new view to the contentView;
[cell.contentView addSubview:view];
Hope this information is helpful.

UIImageView in UICollectionViewCell in UITableViewCell bug

The settings for the UICollectionView were defined using IB (ie scroll direction: horizontal, etc), and was embedded in UITableViewCell using IB.
UICollectionViewCell displays, images display, however, images are stacked on top of one another, instead of one image per one cell with fidelity.
I made individual UIImageView for each picture as instance variables, and same occurred using if and switch statements in the cellForItemAtIndexPath message.
Since IB was used, it may be a stretch to identify the bug, however, would you please help to identify the bug in case it is obvious from the code? Thanks.
#implementation AccountTableViewCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
imageArray = #[[UIImage imageNamed:#"image1.png"], [UIImage imageNamed:#"image2.png"], [UIImage imageNamed:#"image3.png"], [UIImage imageNamed:#"image4.png"], [UIImage imageNamed:#"image5.png"]];
self.oCollectionView.dataSource = self;
[self.oCollectionView setFrame:self.contentView.frame];
[self.contentView addSubview:self.oCollectionView];
self.oCollectionView.backgroundColor = [UIColor clearColor];
[self.oCollectionView reloadData];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return imageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"accountCell" forIndexPath:indexPath];
UIImageView* iv = [[UIImageView alloc] init];
[cell.contentView addSubview:iv];
[iv setFrame:cell.contentView.frame];
iv.image = imageArray[indexPath.row];
return cell;
}
#end
It's because you keep on adding an UIImageView to the cell each time it's dequeued.
Instead, you should subclass the UICollectionViewCell (let's call it "MYCollectionViewCell", add a UIImageView to the cell subclass in the storyboard and set the UIImageView as an outlet on the subclass.
Then, within cellForItemAtIndexPath, set that imageView's image like so:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"accountCell" forIndexPath:indexPath];
cell.imageView.image = imageArray[indexPath.row];
return cell;
}

uicollection view datail view

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...
}