views:

469

answers:

1

I have custom UITableViewCells (created in interface builder) which are used for a static UITableView which for the sake of argument I will call the AddUserTableViewController (i.e. I do not use autoreleased cells as I only have 5 rows).

In the top cell (used for the user name) I have embedded a UIButton which I use to open an UIActionSheet which in turn allows an UIImagePicker to set an image on the UIButton (i.e. [button.imageView.image = imageFromImagePicker]). The button has a default image (added in IB) which says "add image".

The image on the button is set without issue and remains in place even when I navigate to the detail page of any of the table cells with the exception of the user name cell which contains the button. When I select this cell the image dissapears and does not reappear once I navigate back to the AddUserTableViewController from the "add name" detail view. The "add image" default image, referred to above, is displayed.

I have tried many strategies amongst which have been:

1) using [self.tableView reloadData], [button.imageView setNeedsDisplay], [self.view setNeedsDisplay] in the viewWillAppear method;

2) using the above methods in the cellForRowAtIndexPath method;

3) using the above methods in the overriden willDisplayCell:forRowAtIndexPath: method;

When I place debugging NSLog statements in the program I can see that the button.imageView.image property is still set to the image selected using the UIImagePicker but it is not displayed (the default image from the nib is displayed).

As I mentioned above, this only happens if I navigate away from the AddUserTableViewController by selecting the UITableViewCell within which the UIButton is embedded.

I am currently at a loss as to what to do and would be extremely grateful for any assistance that anyone could offer. I just need to find a way to update the image on the embedded UIButton in the above-mentioned circumstances.

I've added the a section of the code from the cellForRowAtIndexPath: method just for illustrative purposes -

//populates the personal info section
if (section == kPersonalInfoAddSection) {

    //Covers the add image button and the name adding field
    if (row == kNameRow) {


        UILabel *nameLabel = (UILabel *)[nameCell viewWithTag:kMainTextTag];
        UILabel *reqLabel = (UILabel *) [nameCell viewWithTag:kSubTextTag];
        //UIButton *imageButton = (UIButton *) [nameCell viewWithTag:kImageTag];

        if (mainUser.imagePath != nil) {

            UIImage *img = [UIImage imageWithContentsOfFile:mainUserImagePath];
            imageButton.imageView.image = img;
            [imageButton.imageView setNeedsDisplay];
            //[imageButton setNeedsDisplay];
            //[nameCell setNeedsDisplay];
            NSLog(@"************************** Added the BESPOKE image (%@)to the image button", img);
        }
        else {
            NSLog(@"************************** Added the DEFAULT image (%@)to the image button", addUserImage);
            imageButton.imageView.image = addUserImage;
        }


        UIColor *blackText = [[UIColor alloc] initWithWhite:0 alpha:1];


        NSString *firstName;

        if (mainUser.firstName){

            nameLabel.textColor = blackText;
            firstName = mainUser.firstName;

        }
        else {

            nameLabel.textColor = reqLabel.textColor;
            firstName = NAME_STRING;
        }

        NSString *lastName = (mainUser.lastName)? mainUser.lastName : EMPTY_STRING;


        NSString *name = [[NSString alloc] initWithFormat:@"%@ %@", firstName, lastName];

        nameLabel.text = name;
        reqLabel.text = REQUIRED_STRING;

        [blackText release];

        return nameCell;

    }
A: 

In the cellForRowAtIndexPath are you doing something that would 'unset' the image?

willcodejavaforfood
No. Please note that I have amended the original question to include a section of the code from the cellForRowAtIndexPath: method.
Urizen