Reputation: 41
I built a CollectionView controller with fixed imageView in 4 cells. I'd like the imageView to change on cell selection. Could you help me with this one?
Thank you
here's my code
CollectionView controller .m
....
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return 4;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImage *image;
int row = [indexPath row];
image = [UIImage imageNamed:@"ICUbedGREEN.png"];
myCell.imageView.image = image;
return myCell;
}
I'd like the image to change on press but I cannot figure it out...
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"ICUbedRED.png"]];
cell.imageView.image = .....
NSLog(@"elementselected");
}
Upvotes: 1
Views: 3689
Reputation: 1255
It can be most stupid answer to your questions but it worked for me. I dont know how to work with KEy value paths as stated by Samuel.
Basically i made a NSMutableArray to store the status of icons, red or green..YES or NO..
selState = [[NSMutableArray alloc] initWithObjects:@"NO",@"NO",@"NO",@"NO",nil ];
and then in "ItemForIndexPath" method, checked the value to set image for that item
if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"NO"]) {
image = [UIImage imageNamed:@"ICUbedGREEN.png"];
}
else
{
image = [UIImage imageNamed:@"ICUbedRED.jpg"];
}
When item is selected, using IndexPath, altered the value of NO into YES or vice versa..
if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"NO"]) {
[selState replaceObjectAtIndex:indexPath.row withObject:@"YES"];
}
else if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"YES"]) {
[selState replaceObjectAtIndex:indexPath.row withObject:@"NO"];
}
And then updated the collection View
[self.collectionView reloadData];
ALL CODE HERE
@interface ViewController (){
NSMutableArray *selState;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
selState = [[NSMutableArray alloc] initWithObjects:@"NO",@"NO",@"NO",@"NO",nil ];
}
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return 4;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImage *image;
if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"NO"]) {
image = [UIImage imageNamed:@"ICUbedGREEN.png"];
}
else
{
image = [UIImage imageNamed:@"ICUbedRED.jpg"];
}
myCell.imageView.image = image;
return myCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"NO"]) {
[selState replaceObjectAtIndex:indexPath.row withObject:@"YES"];
}
else if ([[selState objectAtIndex:indexPath.row] isEqualToString:@"YES"]) {
[selState replaceObjectAtIndex:indexPath.row withObject:@"NO"];
}
[self.collectionView reloadData];
}
@end
Thanks
EDITING---->>
the above code in ItemForIndexPath can also be written in
image = [[selState objectAtIndex:indexPath.row] isEqualToString:@"NO"] ?
[UIImage imageNamed:@"ICUbedGREEN.png"] : [UIImage imageNamed:@"ICUbedRED.jpg"];
END EDITING
Upvotes: 1
Reputation: 401
in your subclass for UICollectionViewCell implement the KVO listening method. Make sure you set multiselection to true if you want it to be deselected.
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString@"selected"])
{
// check value of self.selected to know what you are currently.
// change picture of image here
}
}
in your cells init method you will need to add yourself as a listener so
[self addObserver:self forKeyPath:@"selected" options:nil context:nil];
Upvotes: 1