1

I have a custom UITableViewCell that contains several labels and an image view that can be one of three icons depending on a value in each item represented by the cell. All of my labels are updating properly, but the value of the image name is always nil. The image name is declared as a property of type NSString.

The custom cell is called FavoriteCell and all the values are setting properly except the NSString value "imgName"

FavoriteCell.h:

@interface FavoriteCell : UITableViewCell

@property (nonatomic, strong) UILabel *lblMainTitle;
@property (nonatomic, strong) UILabel *lblGaugeID;
@property (nonatomic, strong) UILabel *lblGaugeLastUpdate;
@property (nonatomic, strong) UILabel *lblGaugeHeight;
@property (nonatomic, strong) UILabel *lblGaugeCFS;
@property (nonatomic, strong) UIImage *bgImage;
@property (nonatomic, strong) UIImageView *goodToGoImage;
@property (nonatomic, strong) NSString *imgName;

@end

FavoriteCell.m:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    bgImage = [UIImage imageNamed:@"tableCellBG.png"];
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        UIColor *transparentBG = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];

        CGSize size = self.contentView.frame.size;
        //self.backgroundView = [[UIImageView alloc] initWithImage:bgImage];
        self.lblMainTitle = [[UILabel alloc] initWithFrame:CGRectMake(8.0, -10, size.width-20, size.height-40)];
        [self.lblMainTitle setFont:[UIFont boldSystemFontOfSize:15]];
        [self.lblMainTitle setTextAlignment:NSTextAlignmentLeft];
        [self.lblMainTitle setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
        [self.lblMainTitle setBackgroundColor:transparentBG];

        self.lblGaugeID = [[UILabel alloc] init];

        self.lblGaugeLastUpdate = [[UILabel alloc] initWithFrame:CGRectMake(9, 14, size.width-40, size.height)];
        [self.lblGaugeLastUpdate setFont:[UIFont systemFontOfSize:14]];
        [self.lblGaugeLastUpdate setBackgroundColor:transparentBG];

        self.lblGaugeHeight = [[UILabel alloc] initWithFrame:CGRectMake(8, 45, size.width-40, size.height)];
        [self.lblGaugeHeight setFont:[UIFont boldSystemFontOfSize:21]];
        [self.lblGaugeHeight setBackgroundColor:transparentBG];

        self.lblGaugeCFS = [[UILabel alloc] initWithFrame:CGRectMake(120, 45, size.width-40, size.height)];
        [self.lblGaugeCFS setFont:[UIFont boldSystemFontOfSize:21]];
        [self.lblGaugeCFS setBackgroundColor:transparentBG];

        NSLog(@"IMAGE NAME: %@\n", imgName); // imgName is nil

        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 40, 40, 40)];
        [imgView setImage:[UIImage imageNamed:imgName]];

        [self.contentView addSubview:lblMainTitle];
        [self.contentView addSubview:lblGaugeHeight];
        [self.contentView addSubview:lblGaugeLastUpdate];
        [self.contentView addSubview:lblGaugeCFS];
        [self.contentView addSubview:imgView];

        self.editingAccessoryType = UITableViewCellAccessoryDetailButton;
    }
    return self;
}

The values are being set in my table view controller's cellForRowAtIndexPath method shown here. For now, I'm just hard coding a value for testing. The hard coded value is not carrying over to the custom cell.

-(FavoriteCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    FavoriteCell *cell = (FavoriteCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
    if(cell == nil){
        cell = [[FavoriteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    Favorite *fave = [allFavoriteObjects objectAtIndex:indexPath.row];

    NSString *currFeet = [NSString stringWithFormat:@"%.02fft", [fave.currentFeet doubleValue]];
    NSString *currFlow = [NSString stringWithFormat:@"%dcfs", [fave.currentCFS intValue]];


    [cell setImgName:@"thumbsUp.png"];
    [cell.lblMainTitle setText:[fave stationRealName]];
    [cell.lblGaugeID setText:[fave stationIdentifier]];
    [cell.lblGaugeCFS setText:currFlow];
    [cell.lblGaugeHeight setText:currFeet];
    return cell;
}

Have I missed something obvious? Thanks!

1
  • What is goodToGoImage used for? If you want to change your custom UIImageView later, you probably want to use a property. In the init method you just create a UIImageView then set the image on it, but don't save the pointer to the view anywhere. (Perhaps that's what goodToGoImage is for?) Commented Oct 28, 2013 at 16:42

2 Answers 2

1

that is normal because you code is running when you don't have set the imgName, you set it then that your init code is run. fox fix it remove the code UIImage and the NSString from you custom cell and set it into your imageView then when you make the cell into the -tableView:cellForRowAtIndexPath: by imageView.image = [UIImage imageNamed:@"thumbsUp.png"];

Sign up to request clarification or add additional context in comments.

14 Comments

Using the standard imageView property of a UITableViewCell apparently does not allow for custom size and placement.
you don't understand what i means
Right, he's creating a separate UIImageView in the init method and then adding it as a subview to contentView
yes but he have to do is create a UIImageView in the init, and then into the tableView:cellForRowAtIndexPath: set its image (and not into the init like he do, because when the init run the variable image is not set)
I'm explain really bad but is normal that this the string imgName is nil because is set then the init
|
0

Well, upon first glance, the moment at which you are saying imgName logs as nil seems to be because you are not actually setting the imgName until after initWithStyle is called. Or is there something I'm missing about what you're expecting to happen?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.