I have a UICollectionView , Under that collection view i have custom UICollectionViewCell with A Image view and A label. I am trying to change color of label text of selected cell. But every time i am changing the color of label text, its changing in multiple cells . Can anyone help me with the issue?
The code i am using :
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *te =[self.smilyObject objectAtIndex:indexPath.row];
self.wallPostMood =[te objectForKey:#"title"];
NSLog(#"%#",self.wallPostMood);
EffectsCollectionViewCell *efCell = (EffectsCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
efCell.title.textColor = [UIColor orangeColor];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
EffectsCollectionViewCell *efCell = (EffectsCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
efCell.title.textColor = [UIColor grayColor];
}
I also have tried :
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *te =[self.smilyObject objectAtIndex:indexPath.row];
self.wallPostMood =[te objectForKey:#"title"];
NSLog(#"%#",self.wallPostMood);
NSArray* visibleCellIndex = collectionView.indexPathsForVisibleItems ;
for(NSIndexPath * path in visibleCellIndex){
EffectsCollectionViewCell *efCell = (EffectsCollectionViewCell *)[collectionView cellForItemAtIndexPath:path];
if([efCell.title.text isEqualToString:self.wallPostMood]){
// NSLog(#"%#",efCell);
// NSLog(#"got");
efCell.title.textColor = [UIColor orangeColor];
}else{
efCell.title.textColor = [UIColor grayColor];
}
}
}
Related
I'm currently facing a problem where my collectionView changes the order of my items when they scroll off the screen and switches the selected cell. At the moment they're organized so that they scrolls vertically (and there are two items side by side):
Here is my - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath method:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"FriendCell";
FriendsCell *cell = (FriendsCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
//Friend Object
PFUser *friend = [self.friendsArray objectAtIndex:indexPath.item];
cell.friendId = currentFriend.objectId;
//Setup Cell
if (!cell.isSelected) {
cell.layer.backgroundColor = [UIColor whiteColor].CGColor;
} else {
cell.layer.backgroundColor = FlatGreen.CGColor;
}
//Set Profile Image
if (!cell.profileImageView) {
cell.profileImageView = [UIImageView new];
cell.profileImageView.image = [UIImage imageNamed:#"ProfileImagePlaceholder"];
[cell addSubview:cell.profileImageView];
}
//Set Username
if (!cell.usernameLabel) {
//Set Username Label
cell.usernameLabel = [UILabel new];
cell.usernameLabel.text = friend[#"username"];
cell.usernameLabel.textColor = [UIColor lightGrayColor];
[cell addSubview:cell.usernameLabel];
} else {
if (cell.isSelected) {
cell.usernameLabel.textColor = [UIColor whiteColor];
} else {
cell.usernameLabel.textColor = [UIColor lightGrayColor];
}
}
return cell;
}
didSelectItemAtIndexPath:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//Determine the selected friend by using the indexPath
PFUser *selectedFriend = [self.friendsArray objectAtIndex:indexPath.item];
//Add the selected friend's objectId to our array
[self.friendsToAdd addObject:selectedFriend.objectId];
//Show Selected View
FriendsCell *currentCell = (FriendsCell *)[collectionView cellForItemAtIndexPath:indexPath];
//Animate
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
//Update
currentCell.layer.backgroundColor = FlatGreen.CGColor;
currentCell.profileImageView.alpha = 0.0;
currentCell.usernameLabel.textColor = [UIColor whiteColor];
} completion:nil];
}
didDeselectItemAtIndexPath:
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
//Determine the selected friend by using the indexPath
PFUser *deselectedFriend = [self.friendsArray objectAtIndex:indexPath.item];
//Remove the deselected friend's objectId from our array
[self.friendsToAdd removeObject:deselectedFriend.objectId];
//Show Deselected View
FriendsCell *currentCell = (FriendsCell *)[collectionView cellForItemAtIndexPath:indexPath];
//Animate
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
//Update Card
currentCell.layer.backgroundColor = [UIColor whiteColor].CGColor;
currentCell.profileImageView.alpha = 1.0;
currentCell.usernameLabel.textColor = [UIColor lightGrayColor];
} completion:nil];
}
If anyone can spot why this is happening, I'd really appreciate it!
Try
dequeueReusableCellWithReuseIdentifier:nil
How to change iOS UICollectionViewCell background outside of the cell?
For instance, I have a view controller which implements UICollectionViewDelegate method:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:MY_CELL_REUSE_IDENTIFIER];
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MY_CELL_REUSE_IDENTIFIER forIndexPath:indexPath];
if (condition)
{
[self actionOne];
cell.backgroundColor = [UIColor redColor];
}
else
{
[self actionTwo];
cell.backgroundColor = [UIColor greenColor];
}
return YES;
}
Breakpoints inside of if (condition) statement shows, that desired lines are executed. But, sadly, cells background stays the same...
What is more, I am successfully doing practically the same thing id UICollectionViewDataSource method:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MY_CELL_REUSE_IDENTIFIER forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
}
What am I doing wrong?
Try cellForRowAtIndexPath in place ofdequeueReusableCellWithReuseIdentifier.
EDIT: Only in shouldSelectItemAtIndexPath.
Explanation: dequeueReusableCellWithReuseIdentifier will get you a new cell, that you change the background color of, but that cell never gets added to the table so you won't see it. When you use cellForRowAtIndexPath, you will get a cell from the table to edit.
This should do it:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
if (condition)
{
[self actionOne];
cell.backgroundColor = [UIColor redColor];
}
else
{
[self actionTwo];
cell.backgroundColor = [UIColor greenColor];
}
}
I can change my collection view cell background color in cellforitematindexpath but not in didselectitematindexpath using the code below (i only change the color in one place however). How does this happen? I know that didselectitematindexpath is being called.
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"CollectionView" forIndexPath:indexPath];
cell.layer.borderWidth=1.0f;
cell.layer.borderColor=[UIColor grayColor].CGColor;
cell.label.text = #"test"
cell.contentView.backgroundColor = [UIColor redColor];
return cell;
}
-(void)collectionView:(UICollectionView *)cv didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"selected cell");
CollectionViewCell *cell = [self collectionView:cv cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor redColor];
}
Several things wrong here.
You're using the wrong method to get the selected cell. Never call the delegate method directly, instead ask the collection view for the cell directly like this:
CollectionViewCell *cell = [cv cellForItemAtIndexPath:indexPath];
You're modifying the cell in a place where those modifications will not persist. When you scroll and that cell gets reused, the color will get wiped out. Instead you should call reloadItemsAtIndexPaths: when the cell gets selected, and have some code in collectionView:cellForRowAtIndexPath that checks the selected property of the cell and sets the color you want based on that. That way, the layout will remain consistent when you scroll and cells are reused.
use this..
self.collectionView.allowsMultipleSelection = YES;
then..
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath**
{
static NSString *identifier = #"Cell";
RecipeViewCell *cell = (RecipeViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipeImages[indexPath.section] objectAtIndex:indexPath.row]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo-frame-2.png"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo-frame.png"]];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (shareEnabled) {
NSString *selectedRecipe = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
[selectedRecipes addObject:selectedRecipe];
}
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (shareEnabled) {
NSString *deSelectedRecipe = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
[selectedRecipes removeObject:deSelectedRecipe];
}
}
I want ot set a border to a selected cell, i save a cell property that represents the selected one and manipulate it:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSArray *eyeArray = [self eyesArrayConfigure][indexPath.row];
if (indexPath.row==5) {
int r = arc4random() % 6;
eyeArray = [self eyesArrayConfigure][r];
}
[borderedCell.contentView.layer setBorderWidth:0.0f] ;
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
borderedCell = cell;
[borderedCell.contentView.layer setBorderColor:self.view.tintColor.CGColor];
[borderedCell.contentView.layer setBorderWidth:3.0f];
}
and the cellForView: (Im usinng 2 types of cell identifiers because one cell contains a label - "Random cell":
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row ==5) {
UICollectionViewCell *randomCell =[collectionView
dequeueReusableCellWithReuseIdentifier:#"randomCell"
forIndexPath:indexPath];
randomCell.backgroundColor = [UIColor purpleColor];
borderedCell = randomCell;
[borderedCell.contentView.layer setBorderColor:self.view.tintColor.CGColor];
[borderedCell.contentView.layer setBorderWidth:3.0f];
return randomCell;
}
UICollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
NSArray *eyeArray = [self eyesArrayConfigure][indexPath.row];
myCell.backgroundView = [[UIImageView alloc] initWithImage:eyeArray[1]];
return myCell;
}
What i get is if a click one cell it will be fine till i scroll and then i get weird behaviour when couple of cells might be with the border.
Thanks for the help.
The solution is to use a view model. You are already doing this for your cells' background views. Apply the same concept for the border.
#interface MyCollectionView ()
#property (nonatomic) NSInteger selectedRow;
#end
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//update self.selectedRow and either reload entire collection view or just the currently selected and previously selected.
self.selectedRow = indexPath.row;
[collectionView reloadData];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
CGFloat borderWidth = (indexPath.row == self.selectedRow) ? 3.0 : 0.0;
[myCell.contentView.layer setBorderWidth:borderWidth];
return myCell;
}
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?
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];
}
If you subclassed the cell class, put this in your .m file
- (void)setSelected:(BOOL)selected
{
if(selected)
{
self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
}
else
{
self.backgroundColor = [UIColor whiteColor];
}
}
try this
cell.selectedBackgroundView.backgroundColor = [UIColor greenColor];
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];
}