need help about display image on the cell,

Discussion in 'iOS Programming' started by tranvutuan, Dec 23, 2011.

  1. tranvutuan, Dec 23, 2011
    Last edited by a moderator: Dec 25, 2011

    tranvutuan macrumors member

    Dec 19, 2011
    Hi everyone
    Iam working on UITableView and doing an customization for a cell of each row.However, my image can not be displayed at all
    What I am having so far is
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        static NSString *CellIdentifier = @"reUsedDetailCell";
        DetailCell *cell    = (DetailCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // this is my customized cell
        if ( cell == Nil ) {
            cell        =   [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; // do initialization
        return cell;
    At DetailCell.m:
    //  DetailCell.m
    #import "DetailCell.h"
    @implementation DetailCell
    @synthesize itemImg;  // itemImg is type of UIImageView
    @synthesize itemName;
    @synthesize itemDesp;
    @synthesize itemSize;
    @synthesize itemPrice;
    @synthesize itemInfo;
    @synthesize addItem;
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            self.backgroundColor = [UIColor redColor];
            itemImg = [[UIImageView alloc] init];
            [itemImg setImage:[UIImage imageNamed:@"gear_24.png"]] ;  
            //[self.contentView addSubview:itemImg]; // I comment this out because itemImg is already in DetailCell by using Storyboard
        return self;
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
        [super setSelected:selected animated:animated];
        // Configure the view for the selected state
    Can anybody point it out if I have made a mistake somewhere. I appreciate that


    Anyone comes up with an idea please help.....
  2. ArtOfWarfare, Dec 25, 2011
    Last edited by a moderator: Dec 26, 2011

    ArtOfWarfare macrumors 604


    Nov 26, 2007
    Now... I haven't done this kind of thing before... but you say you're using a storyboard. I'm curious whether you've actually hooked up your image view to anything in your storyboard. Don't you need to have itemImg listed as an IBOutlet in your header file (which you may have done, IDK because you neglected to include your header,) and then have that outlet connected to the image view in your prototype cell in storyboard?

    I'm only working on my first project utilizing storyboard now, so I might be off on what getting an image to show up in a cell in storyboards entails...
  3. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    You are initing your UIImageView again, while it's on your storyboard.
    Also, doing filling methods in the init method is not good.
    Because you are not sure all the outlets are set to it's corresponding imageview/label/textview, maybe the XIB is still loading while you are calling this init method.
    best practice is to make custom init methods and then in the special methods for UITableviewCell or UIViewController, when the nib is loaded (or XIB if you want).
    Then set the image view.

    Also, to come back to my first line, you are telling the xib to make the UIImageView, but then overriding it with another alloc init, which creates another pointer if i'm not mistaking.. So therefor, your new pointer won't have a frame and therefore not show an image.

Share This Page