Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
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
Code:
- (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:
Code:
//
//  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
}

@end

Can anybody point it out if I have made a mistake somewhere. I appreciate that

------------

Anyone comes up with an idea please help.....
 
Last edited by a moderator:
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...
 
Last edited by a moderator:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.