I'm building an app in iOS and I want the Cells in my CollectionView to highlight when touched, pretty much like the normal buttons. How can I achieve this in the didSelectItemAtIndexPath:(NSIndexPath *)indexPath method?
Asked
Active
Viewed 1.5k times
6
-
Change the background color of that cell. – user1673099 Oct 24 '13 at 09:45
4 Answers
7
Try something like this:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
.....
if (cell.selected) {
cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; // highlight selection
}
else
{
cell.backgroundColor = [UIColor clearColor]; // Default color
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; // //cell.lblImgTitle.text = @"xxx";
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor clearColor];
}

acoustic
- 4,557
- 3
- 19
- 18

Calin Chitu
- 1,099
- 7
- 13
-
I solved it by changing the Alpha of the picture in the cell in the didHighlightItemAtIndexPath and didUnhighlightItemAtIndexPath methods. Anyway, I'll set your answer as the right answer. Thanks. – diogo.appDev Oct 24 '13 at 10:37
-
1why don't you post how did you solve your issue in the answer? It will be helpful for others ...diogo-appdev – Narasimha Nallamsetty Mar 26 '15 at 18:16
0
try this
cell.selectedBackgroundView.backgroundColor = [UIColor greenColor];

Vaisakh
- 2,919
- 4
- 26
- 44
0
why can not change the background color through the cocoa pods?I new a user-defined collectionView cell class`
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
CHSMenuControlCell *cell = (CHSMenuControlCell*)[collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; // //cell.lblImgTitle.text = @"xxx";
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
CHSMenuControlCell *cell = (CHSMenuControlCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor clearColor];
}