So I have a list of items in a UICollectionView
and there is a UIButton
which has it's default and selected UIImage
's set to a light grey heart and a black heart.
My aim is to allow users to add items to a list of favourites. I've added a column to the database called "favourite" and it is of type boolean.
Customer taps heart in cell of item. A method is triggered which users the object id of the item that is passed along as the titleLabel of the UIButton
to grab the item from the database and update the boolean value of the favourite column to "True".
Method for favourite button:
- (void)addFavouriteButtonTapped:(UIButton *)sender
{
NSLog(@"add to favourites button tapped %@", [[sender titleLabel] text]);
// Set data base favaourite status to 1 (yes)
PFQuery *query = [PFQuery queryWithClassName:@"Garments"];
[query getObjectInBackgroundWithId:[[sender titleLabel] text] block:^(PFObject *object, NSError *error) {
object[@"favourite"] = @YES;
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[[[UIAlertView alloc] initWithTitle:@"Add Favouite"
message:@"Item successfully added to favourites"
delegate:_thisController
cancelButtonTitle:@"Ok"
otherButtonTitles: nil] show];
} else {
NSLog(@"Something went wrong");
}
}];
}];
}
Then in my cellForItemAtIndexPath
I have an if statement which sets the selected state of the UIButton
.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
NSLog(@"cellForItemAtIndexPath");
// other cell info here deleted to make things clearer
_addFavouriteButton = [cell addFavouriteButton];
// if database favourites status is equal to one then set highlighted to (yes)
if ([object valueForKey:@"favourite"] == [NSNumber numberWithBool:YES]) {
[_addFavouriteButton setSelected:YES];
NSLog(@"SETTING SELECTED");
} else if ([object valueForKey:@"favourite"] == [NSNumber numberWithBool:NO]) {
[_addFavouriteButton setSelected:NO];
}
[[_addFavouriteButton titleLabel] setText:[object objectId]];
[_addFavouriteButton addTarget:_thisController action:@selector(addFavouriteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Now when I leave the view and everything is destroyed then return back to it again, black hearts appear where ever I selected a favourite. This is fine. The problem I am having is when I actually make the selection of the item to add as a favourite the heart is updated and remains grey until I leave the UICollectionView
and return.
I've tried reloadData
in the main queue of the addFavouriteButtonTapped: method
I've also tried reloading just the cell.
None of this works.
I can set the UIButton
's state to selected but as I scroll the selection doesn't keep position and moves to other cell UIButton
's and off the cell with the button I actually selected. Only way to fix this is to leave the few so the we have a fresh UICollectionView
that uses the if statement from my database to determine whether a cell is selected or not.
**My custom cell:**
@interface VAGGarmentCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet PFImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *addFavouriteButton;
@property (weak, nonatomic) IBOutlet UITextView *title;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@end
Question:
I've done the bulk of what needs to be done and can use the favourites column later on when building the page that lists items in the customers favourite list. But how do I refresh the UICollectionView
right after the button was tapped and successful update in order to display the UIButton
's selected state?