how to change collection view cell background colour when it is selected? - ios

This code is I used if cell is selected then the background colour have to change in image view it is placed inside the collection view cell.
But its not working
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (cell.selected) {
cell.img_cell.backgroundColor = [UIColor colorFromHexString:#"#ffc400"]; // highlight selection
}
else
{
cell.backgroundColor = [UIColor clearColor]; // Default color
}
NSLog(#"Selected section>> %#",[arr_images objectAtIndex:indexPath.row]);
// cell.backgroundColor=[UIColor colorFromHexString:#"#ffc400"];
}

now its working i removed the if condition and tried using cellForItemAtIndexPath .
cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.img_cell.backgroundColor = [UIColor colorFromHexString:#"#ffc400"]; // high

Related

UILabel Hidden text issue

Well this so weird . I also hesitate to post this question but I haven't got any solution that's why I decided to post it. Here I go.
I have a XIB i have subclassed it in UICollectionView. This is my code in didSelectMethod:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = (CollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
cell.nameLabel.textColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.8];
[cell setSelected:NO];
cell.nameLabel.hidden = NO;
cell.nameLabel.text = #"HHHHHHH";
cell.nameLabel.backgroundColor = [UIColor redColor];
}
Now when I run my code I get correct Output. But when I select any cell then text of my UILabel is hidden I can only see the background colour.
When I select another one then I get back the text of previously selected UILabel.
I haven't written any other code I not able to figure it OUT.
When I select any cell:
When I select another cell:
When I select Previous Cell:
Here is my code for making cells:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// Get reusable cell reference
CollectionViewCell *cell = (CollectionViewCell *)[cv dequeueReusableCellWithReuseIdentifier:#"CollectionViewCell" forIndexPath:indexPath];
[cell updateText];
return cell;
}
This method is in custom Class
- (void) updateText{
self.nameLabel.text = name;
}
Go to your nib/xib file and check the highlighted color weather it is clear or some other color. If it is clear than change as per your requirement.
Capture your selected index in didSelectItemAtIndexPath and reload you collection view. In cellForIndexPath check for your selected index and do the stuff there. Do some thing like below
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (selectedIndex == indexPath.row) {
cell.nameLabel.textColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.8];
[cell setSelected:NO];
cell.nameLabel.hidden = NO;
cell.nameLabel.text = #"HHHHHHH";
cell.nameLabel.backgroundColor = [UIColor redColor];
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath.row;
[collectionView reloadData];
}
It's high probably because your CollectionViewCell is reusable cell. You hidden state is reused for other cells too. You should set it in your cellForIndexPath method.
- (void)updateText
{
self.nameLabel.text = name;
cell.nameLabel.hidden = YES; // set here it's default value
}
Create a Bool Array for each cell and then update the values when any cell is selected i.e in didSelectItemAtIndexPath.
Also inside cellForItemAtIndexPath, you can go as quoted by Vijay except just taking in consideration only one selected cell you can now handle multiple selected cells.
The real issue is that you should have set highlighted color of label to clear color in xib unknowingly. Check your nib and change highlighted property to desired color you want. When a cell is selected the subviews changes their color to their highlighted color set in xib. Hope this will help you. enter image description here

Changing the state of the CollectionView Cell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//NSLog(#"enter category cell");
CategoryViewCell* cell = (CategoryViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[cell.imgCat setImage:[UIImage imageNamed:[categoryImages objectAtIndex:indexPath.row]]];
[cell.labelCatName setText:[[NSString stringWithFormat:#"%#", [catName objectAtIndex:indexPath.row]] capitalizedString]];
if([categories[[NSString stringWithFormat:#"%d",(int)indexPath.row]] isEqual:#YES]) {
//NSLog(#"set border");
cell.layer.borderColor = [UIColor redColor].CGColor;
cell.layer.borderWidth = 3;
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
CategoryViewCell* cell = (CategoryViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
cell.layer.borderWidth = 3;
cell.layer.borderColor = [UIColor redColor].CGColor;
//NSLog(#"%i", (int)indexPath.item);
categories[[NSString stringWithFormat:#"%i", (int)indexPath.item]] = #YES;
}
-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
CategoryViewCell* cell = (CategoryViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
cell.layer.borderColor = [UIColor clearColor].CGColor;
categories[[NSString stringWithFormat:#"%i", (int)indexPath.item]] = #NO;
}
The problem:
The above code will show you a collectionView with cells that are selected at default. However, the states of these selected cells are not selected. So I have to tap twice to deselect them because the first tap is to select them, and second tap is to deselect them.
I have tried to set selected for cell but it doesn't work either. The cell will have a red border whenever the user selected a cell and clearColor when the user deselect the cell.
I tried:
cell.selected = YES;
But this permanently gives a collectionView Cell a red border.
And add it in cellForItemAtIndexPath method still doesn't do the trick.
[self collectionView:collectionView didSelectItemAtIndexPath:indexPath];
CategoryViewCell.m
-(void) setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected];
//NSLog(#"Pass Select Animated");
if (selected) {
self.flagHighlight = NO;
self.selected = NO;
self.layer.borderColor = [UIColor clearColor].CGColor;
self.layer.borderWidth = 3;
} else {
self.flagHighlight = YES;
self.selected = YES;
self.layer.borderColor = [UIColor redColor].CGColor;
self.layer.borderWidth = 3;
}
}
How would I pre-select a cell when the view is loaded programmatically?
Or even better just change the state of the cell being selected.
Thanks in advance.
Ending up answering my own problem.
so I use [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
in cellForItemAtIndexPath.
Serve the purpose nicely.

Changing background color of not highlighted cell in UICollectionView

I managed to set the cell background color when it is tapped on with this code:
- (void) collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath: (NSIndexPath *)indexPath
{
NSLog(#"highlighted cell at index path %ld", (long)indexPath.row);
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor blueColor];
}
but then I have 2 problems
1: I show 6 cells in a view, but then I slide to show the other cells, some of the cells are also highlighted with the blue background
2: After I select cell A and have the background color changed to blue, when I select B, the background color in A does not changed back to white, how could I change it back?
Thanks!
You need to reset the cell properly in your collectionView:cellForItemAtIndexPath: method.
if (/* cell is highlighted */) {
cell.contentView.backgroundColor = [UIColor blueColor];
} else {
cell.contentView.backgroundColor = [UIColor whiteColor]; // use whatever is appropriate
}
This of course requires that you keep track of which cells should be highlighted.
If you only need to have the cell highlighted to the blue background while your finger is touching the cell, you can change the color back to white in the didUnhighlightItemAtIndexPath.
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"unhighlighted cell at index path %ld", (long)indexPath.row);
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor whiteColor];
}
This makes it so that other cells do not get recycled with the same background color (problem 1) and cell A immediately returns to the default color after lifting your finger before tapping cell B (problem 2).
See this question and its answers for more details.

UICollectionViewCell Selection selects/highlights every 5 cells

I have a horizontal collectionview with code to highlight/color the cell selected. It highlights the cell that was selected, but then every 5 cells after also get highlighted. Any idea what is going on?
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
for(int x = 0; x < [cellArray count]; x++){
UICollectionViewCell *UnSelectedCell = [cellArray objectAtIndex:x];
UnSelectedCell.backgroundColor = [UIColor colorWithRed:0.2 green:0.5 blue:0.8 alpha:0.0];
}
UICollectionViewCell *SelectedCell = [cellArray objectAtIndex:indexPath.row];
SelectedCell.backgroundColor = [UIColor colorWithRed:0.2 green:0.5 blue:0.8 alpha:1.0];
cellSelected = indexPath.row;
NSLog(#"%i", cellSelected);
}
That happens because cells are reused when you scroll. You have to store the "highlighted" status for all rows in your model (for example in an array or NSMutableIndexSet), and in collectionView:cellForItemAtIndexPath: set the background color of the cell according to the status for that row.
In didSelectItemAtIndexPath it should be sufficient to set the color of the newly selected
and the previously selected cell.
Update: If only one cell can be selected at a time, you just have to remember the
index path of the selected cell.
Declare a property selectedIndexPath for the currently highlighted row:
#property (strong, nonatomic) NSIndexPath *selectedIndexPath;
In didSelectItemAtIndexPath, unhighlight the previous cell, and highlight the new cell:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (self.selectedIndexPath != nil) {
// deselect previously selected cell
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:self.selectedIndexPath];
if (cell != nil) {
// set default color for cell
}
}
// Select newly selected cell:
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
if (cell != nil) {
// set highlight color for cell
}
// Remember selection:
self.selectedIndexPath = indexPath;
}
In cellForItemAtIndexPath, use the correct background color:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Identifier" forIndexPath:indexPath];
if ([self.selectedIndexPath isEqual:indexPath) {
// set highlight color
} else {
// set default color
}
}
I think you use reusable cell for collection - there is the point.
You shell to set default background color before reuse cell.

Highlight collection cell when selected

I am trying to highlight a selected collection cell in UICollectionView with yellow border so user can see which one is currently selected. I tried this:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
FilterCell *filterCell = (FilterCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"FilterCell" forIndexPath:indexPath];
filterCell.window.backgroundColor = [UIColor yellowColor];
filterCell.backgroundColor = [UIColor yellowColor];
NSLog(#"hello");
}
There are 2 empty pixels around UIImageView inside UICollectionViewCell so it should work but it doesn't.
It is logging "hello" but the border stays black. See this screenshot:
You are getting the cell in the wrong way
FilterCell *filterCell = (FilterCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"FilterCell" forIndexPath:indexPath];
will dequeue a cell which is not used right now or allocate a new one with the specified identifier.
Use
FilterCell *filterCell = (FilterCell *)[collectionView cellForItemAtIndexPath:indexPath];
instead.
Anyway a cleaner solution would be to set the backgroundView and selectedBackgroundView properties of the cell, without touching the backgroundProperty color (that will stay clear as default). In this way you can avoid the delegate method and achieve the same behavior.
Do a reloadItemsAtIndexPaths: there instead, then in cellForItemAtIndexPath, check if [[collectionView indexPathsForSelectedItems] containsObject:indexPath] If true, change the cell's attributes there.
This may help you:
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"YOUR_FILE_NAME.png"]];
-This code may help you to change background colour of selected cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cvCell" forIndexPath:indexPath];
if (cell.selected) {
cell.backgroundColor = [UIColor blueColor]; // highlight selection
}
else
{
cell.backgroundColor = [UIColor redColor]; // Default color
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
datasetCell.backgroundColor = [UIColor blueColor]; // highlight selection
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
datasetCell.backgroundColor = [UIColor redColor]; // Default color
}

Resources