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.
Custom tableview cell for displaying different foods
design patterns;mvc;objective c
For what you're doing, both options are wrong actually.Currently, we're asking both the UITableViewDelegate and the UITableViewCell to do too much work.  Your proposed change only shifts more work onto the UITableViewCell, which is already doing too much.Instead, we need a custom class for carrying around our data.  One way or another, the UITableViewDelegate gets a reference to an array of these objects.  This class should have methods and properties that can tell the cell how to appear.In this specific case, one of those properties is:@property BOOL isFavorited;And one of those methods might be:- (void)toggleFavorite {    self.isFavorited = !self.isFavorited;}In the table view's data source, you grab the object out of the array the corresponds to the section/row you're creating the cell for and you ask these custom objects how the cell should look.The cell could have a property as such:@property (nonatomic,assign) BOOL isFavorited;And we implement the setter for it as such:- (void)setIsFavorited:(BOOL)isFavorited {    _isFavorited = isFavorited    NSString *imgName = isFavorited ? @star_full.png : @star_none.png;    [self.favButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];}And in didSelectRowAtIndexPath, we shouldn't be calling cellForRowAtIndexPath: to completely reconstruct the cell.  This wastes a lot of time setting up the cell.  Instead, we use the index path to reach into our original array again and grab out the data object that we used to create the cell in the first place.MyDataObject *dataObject = self.myArrayOfDataObject[indexPath.row];Then from here, we modify the data object:[dataObject toggleFavorite];Now, since we set up cellForRowAtIndexPath: to set up the cell based on our data object, we tell the table to refresh itself (or at least this row) using either:[self.tableView reloadData];Or reload just the individual cell:[self.tableview reloadRowsAtIndexPaths:@[indexPath]                       withRowAnimation:UITableViewRowAnimationNone];