My App consists of 3 views (View 1 (Tableview1), View 2 (Tableview2), View 3 (DetailView)).
I have a custom tableview cell set up with subviews (imageViews and Labels) in my View 2 (Tableview2).
One of these subviews is an ImageView, and contains an image if the cell ID is in a favorites Array. When you select a cellForRowAtIndexpath you are pushed to a new view (View 3 (DetailView)), where you can press a button and make the item a favorite.
When you close that view and go back the image is shown, and everything works fine.
My problem occurs when I try to deselect an item as favorite. The favorite symbol is still shown in the tableView, until i go back to View 1 (Tableview1).
I have tried to call [myTableview reloadData], in View 2 on ViewWillAppear, but the favorites symbol is still there. My favorites Array is saved and loaded with the correct values.
The Array gets updated at ViewWillAppear…
What could be the problem?
Relevant code: EDIT – Posted all relevant code from cellForRowAtIndexPath: Everything is showing right, but the tableview does not get updated if I remove an image from the custom cell… EDIT: Updated with else statement in the for loop to try and remove the favorite images from the items that are no longer in the favorites array.
EDIT EDIT EDIT: Working now, added a break statement in my for loop… Updated code so that it might help others…
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *kategori = [prefs objectForKey:@"kategoriID"];
NSLog(@"KategoriID = %@", kategori);
rowArray = nil;
rowArray = [[NSMutableArray alloc] init];
for(int i=0; i < [listOfItems count]; i++) {
if([kategori isEqualToString:[listOfItems objectAtIndex:i]])
{
//Tilføjer ID
[rowArray addObject:[NSNumber numberWithInt:i-5]];
}
}
int arrayValue = [[rowArray objectAtIndex:indexPath.row] intValue];
cell.primaryLabel.text = [listOfItems objectAtIndex:arrayValue+1];
cell.secondaryLabel.text = [listOfItems objectAtIndex:arrayValue+2];
cell.myImageView.image = [UIImage imageNamed:[listOfItems objectAtIndex:arrayValue+4]];
//Time
cell.timeImageView.image = [UIImage imageNamed:@"03-clock.png"];
cell.timeLabel.text = [listOfItems objectAtIndex:arrayValue+6];
//Views
cell.viewsImageView.image = [UIImage imageNamed:@"04-eye.png"];
cell.viewsLabel.text = [listOfItems objectAtIndex:arrayValue+7];
//Stars
NSString *starsString = [listOfItems objectAtIndex:arrayValue+8];
if ([starsString isEqualToString:@"S0"]) {
cell.starsImageView.image = [UIImage imageNamed:@"0-stars.png"];
}
if ([starsString isEqualToString:@"S0_1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"0_1-stars.png"];
}
if ([starsString isEqualToString:@"S1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"1-stars.png"];
}
if ([starsString isEqualToString:@"S1_1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"1_1-stars.png"];
}
if ([starsString isEqualToString:@"S2"]) {
cell.starsImageView.image = [UIImage imageNamed:@"2-stars.png"];
}
if ([starsString isEqualToString:@"S2_1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"2_1-stars.png"];
}
if ([starsString isEqualToString:@"S3"]) {
cell.starsImageView.image = [UIImage imageNamed:@"3-stars.png"];
}
if ([starsString isEqualToString:@"S3_1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"3_1-stars.png"];
}
if ([starsString isEqualToString:@"S4"]) {
cell.starsImageView.image = [UIImage imageNamed:@"4-stars.png"];
}
if ([starsString isEqualToString:@"S4_1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"4_1-stars.png"];
}
if ([starsString isEqualToString:@"S5"]) {
cell.starsImageView.image = [UIImage imageNamed:@"5-stars.png"];
}
//favorite
NSString *IDString = [listOfItems objectAtIndex:arrayValue];
NSLog(@"ID String: %@", IDString);
if (videoerIFavoritDatabase == 0) {
NSLog(@"Der er ingen videoer i favorit databasen!");
}
else {
for(int i=0; i < [tableviewFavoritArray count]; i++) {
NSLog(@"ID Array Value:%i", i);
if([IDString isEqualToString:[tableviewFavoritArray objectAtIndex:i]])
{
cell.favoriteImageView.image = [UIImage imageNamed:@"favoriteTableview@2x.png"];
NSLog(@"ER favorit:%i = ID%@", i, [tableviewFavoritArray objectAtIndex:i]);
break;
}
else { cell.favoriteImageView.image = [UIImage imageNamed:@"empty.png"];
break;
}
}
}
return cell;
}
You doing all of your cell configuration inside the
if (cell == nil)statement. This code is only executed if the tableView did not return a cell from the dequeue method. What is probably happening is that you are re-using cells and they are not being configured, so the data and appearance remain the same.Your
cellForRowAtIndexPathmethod should have the following structure:if (cell == nil)statementOK, based on your updated code, the problem appears to be that you are setting the favourite image, but you don’t unset it if the cell is no longer a favourite. You need an
elseto set that image to nil if the cell is not a favourite.