Custom UITableView updating appropriately with every label and UIImage, except one - using Core Data and NSFetchedResultsController

StackOverflow https://stackoverflow.com/questions/22956894

Question

I have a UITableViewController that's populated by NSFetchedResultsController and Core Data where the user adds in information into another view, saves and then the UITableView updates.

Everything is working with the exception of one UIImageView in the custom cell.

The user adds in a name, event, sub-event, date, status and optionally a note in another view that's called modally from this UITableViewController with a button in the NavigationBar.

Problem

The notes UITextView has a default value of "Additional Notes :" and I have configured it so that if a user starts typing, it removes that text and if the user taps in that cell but doesnt type anything, it just goes back to "Additional Notes :". If the user adds in a note to the entry, the UITableViewCell is made to display a "pin" image. That works. If the user does not add in a note, there's no PIN image. However, in the scenario below, the pin is still displayed.

Broken Scenario

1) Add a new entry without a note. UITableViewCell does not display PIN. -- Correct.

2) Edit entry to include note. When saved, the UITableViewCell displays a PIN -- Correct.

3) Edit entry again to remove note (which defaults back to the "Additional Notes :" text.) When saved, the PIN is still displayed in the custom cell. -- Incorrect.

4) If you close the app from the multi-tasking, and then re-launch the app, the PIN is then not displayed on that cell.

So it's a case of refreshing.

Here's my cell code:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{    
    TimelineTableViewCell *customCell = (TimelineTableViewCell *)cell;
    Transaction *transaction = [self.fetchedResultsController objectAtIndexPath:indexPath];

    customCell.nameLabel.text = transaction.whoBy.name;
    customCell.amountLabel.text = transaction.item.amount;
    customCell.nameLabel.textColor = [UIColor whiteColor];
    customCell.amountLabel.textColor = [UIColor whiteColor];

    // Events and Subevents
    if ([transaction.subevent.title length] > 0)
    {
        customCell.eventLabel.text = [NSString stringWithFormat:@"%@ (%@)", transaction.occasion.title, transaction.subevent.title];
        customCell.eventLabel.textColor = [UIColor whiteColor];
    }
    else
    {
        customCell.eventLabel.text = transaction.occasion.title;
        customCell.eventLabel.textColor = [UIColor whiteColor];
    }

    // Setting up the cells text and custom circle status images
    if ([transaction.wasReceived boolValue])
    {
        UIImageView *dot = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 14, 15)];
        dot.image=[UIImage imageNamed:@"Greendotwithshadow"];
        [customCell addSubview:dot];
    }
    else
    {
        UIImageView *dot = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 14, 15)];
        dot.image=[UIImage imageNamed:@"Reddotwithshadow.png"];
        [customCell addSubview:dot];
    }

    if (![transaction.notes isEqualToString:@"Additional Notes: "] && [transaction.notes length] > 0)
    {
        NSLog(@"This is a test service announcement");
        UIImageView *pin = [[UIImageView alloc] initWithFrame:CGRectMake(13, 30, 24, 25)];
        pin.image = [UIImage imageNamed:@"pin"];
        [customCell addSubview:pin];

    }

    UIView *customColorView = [[UIView alloc] init];
    customColorView.backgroundColor = [UIColor darkGrayColor];
    customCell.selectedBackgroundView = customColorView;

}

It's the last if statement there thats not working as it should be. However, the other if statements are working. If I create an entry with the status "Received", it adds a Red dot to the cell. If I edit that entry to "Given", a green dot is shown instead when the UITableView is reloaded. I don't understand why the pin image is not acting as it should though.

In my viewWillAppear, I have no reload data, but at the same time, I tried:

NSError *error;
[self.fetchedResultsController performFetch:&error];
[self.timelineTableView reloadData];

with no difference.

The Notes is a UITextView in the entry and the Status is a UISegmentedControl.

In the save method of the edit entry, this is my code for the Notes and Status:

self.selectedTransaction.wasReceived = @(self.editingIsReceivedSegment.selectedSegmentIndex == 0);

self.selectedTransaction.notes = self.editingNotesTextView.text;

The image for the status acts perfectly, but the notes does not.

Any thoughts or suggestion with this would really be appreciated.

Was it helpful?

Solution

Try this,

UIImageView *pin;
if (![transaction.notes isEqualToString:@"Additional Notes: "] && [transaction.notes length] > 0)
{
    NSLog(@"This is a test service announcement");
    if ([customCell viewWithtag:8888]) {
        pin = (UIImageView *)[customCell viewWithtag:8888];
    } else {
        pin = [[UIImageView alloc] initWithFrame:CGRectMake(13, 30, 24, 25)];
        pin.image = [UIImage imageNamed:@"pin"];
        pin.tag = 8888;
        [customCell addSubview:pin];
    }
} else {
     if ([customCell viewWithtag:8888]) {
        pin = (UIImageView *)[customCell viewWithtag:8888];
        [pin removeFromSuperview];
    } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top