In my iPhone app I have a table view where I add a tick image to a cell if that objects 'isConfirmed' value is true. When entering the detailed view I can edit the confirmed value, and upon popping back to the main table view I need to see the update and not only when I view the main table from a fresh.
So I am using this code in my tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method`:
UIImageView *tickImg = nil;
//If confirmed add tick to visually display this to the user
if ([foodInfo.isConfirmed boolValue])
{
tickImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ConfirmedTick.png"]];
[tickImg setFrame:CGRectMake(0, 0, 32, 44)];
[cell addSubview:tickImg];
}
else
{
[tickImg removeFromSuperview];
}
What this does it successfully add the tick image to my cells which have a true value for isConfirmed and when going into the detail view of an object and setting it to TRUE and retuning, the tick appears, however I can't get it to work the other, so if the tick is there and I go into the detail view to unconfirmed it, the tick doesn't disappear.
Hope you can help, me with this, thanks.
Are you calling [self.tableView reloadData]; on the VC's viewWillAppear:?
Also, the approach you're using to configure the cell is error-prone. Since the tableView is reusing the cells, you can't be sure what state a cell is in when you dequeue it.
A better approach would be to build the cells consistently:
static NSString *CellIdentifier = @"MyCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// always create a tick mark
UIImageView *tickImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ConfirmedTick.png"]];
tickImg.tag = kTICK_IMAGE_TAG;
tickImg.frame = CGRectMake(0, 0, 32, 44);
[cell addSubview:tickImg];
}
// always find it
UIImageView *tickImg = (UIImageView *)[cell viewWithTag:kTICK_IMAGE_TAG];
// always show or hide it based on your model
tickImg.alpha = ([foodInfo.isConfirmed boolValue])? 1.0 : 0.0;
// now your cell is in a consistent state, fully initialized no matter what cell
// state you started with and what bool state you have
cell.tickImg.alpha = ([foodInfo.isConfirmed boolValue])? 1.0 : 0.0;
straight after adding it as a subview, surely this should do it? However, instead, it gives me the same results that I explained originally. As you say, having a permanent image there and simply switching it alpha state should work, clueless - Josh Kahane 2012-04-03 23:41
This is the code that is executed if [foodInfo.isConfirmed boolValue]
is false:
UIImageView *tickImg = nil;
[tickImg removeFromSuperview];
Clearly this will not work -- tickImg is not pointing to the UIImageView. You need to somehow save the reference to the UIImageView. You could add the tickImg variable to the header of your class or make it a property or something.