Removing Image From Parent View

Go To StackoverFlow.com

0

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.

2012-04-03 21:57
by Josh Kahane


0

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
2012-04-03 22:12
by danh
Presumably in your code sample you have your image view in interface builder? As you don't add it as a subview - Josh Kahane 2012-04-03 23:11
No, I favor that approach, but your code indicates that you build the image view in code, so I did that, too, in my answer. See it - danh 2012-04-03 23:16
oops. i didn't add it as a subview. my bad. will edit. try that code now. i think it will do what you want - danh 2012-04-03 23:17
Problem here is the tag is identical for every cell. How can I make sure each cell has a unique tag - Josh Kahane 2012-04-03 23:33
I've tried incrementing the tag by the indexPath row, so they should all be uniquely tagged, however this has still proved to fail. Currently more functional without the tag, I have added it as a property of my custom cell class and simply, add it as you suggest (minus the tag) and call 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
Blimey, seems just having it as an object in IB fixed it, hm. Well, there you go. Thanks - Josh Kahane 2012-04-03 23:48
Glad it worked. Fyi - we want a constant tag in the code above. Did it not work if you pasted it as-is? Anyway - glad it's working. Happy coding - danh 2012-04-03 23:56


1

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.

2012-04-03 22:12
by Michael Frederick
Ads