I have a custom tableview cell for displaying different foods, with a star next to each name. If the food has been favorited (i.e., it exists in the database), the star is filled in, otherwise it is empty. I'm not sure if the logic that determines the color of the star should go in the view controller or the tableviewcell.
Currently, I have this in the view controller under didSelectRowForIndexPath
:
cell.foodLabel.text= food.name;
if ([cell isStarredForName:food.name])
[cell changeStarToState:StateFull];
else
[cell changeStarToState:StateEmpty];
Also, I use this cell in two different view controllers, so I have to use the code above twice.
In the cell, I have:
- (BOOL) isStarredForName:(NSString*)foodName {
PFInstallation *installation = [PFInstallation currentInstallation];
NSArray *foodList = (NSArray*)[installation objectForKey:@"favorites"];
return ([foodList containsObject:foodName]);
}
- (void)changeStarToState:(StarState)state{
NSString *imgName;
if (state == StateEmpty){
imgName = @"star_none.png";
_currentState = StateEmpty;
}
else {
imgName = @"star_full.png";
_currentState = StateFull;
}
[self.favButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
}
I'm wondering if it's better to just have a [cell handleStarColor]
function in the viewcontroller and have the cell decide whether it needs to fill in the star.