2

I created a custom UITableViewCell , but want to use the standard UIImageView that is in the cell.

I notice that the frame is always zeroed , which is causing me problems

@implementation MyCustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    // the style is "default" style
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
// I need to override the cells image 
    if (self) {
        self.imageView.image = [UIImage imageNamed:@"MyImageToUseInThisCell"]; // The image view frame is {0,0,0,0}
    }
    return self;
}
Mahboob Nur
  • 739
  • 2
  • 9
  • 34
Avba
  • 14,822
  • 20
  • 92
  • 192
  • put a breakpoint in `-initWithStyle:`. does it hit this method? also, kindly include your `-cellForRowAtIndexPath:` (just for clarity). Even if it does hit `-initWithStyle:`, it won't return any frame sizes because the cell hasn't loaded yet which means the `subview`s haven't been created yet (_i.e. are `nil`_). You would be better off with frame specific related stuff in `-layoutSubviews` and as for setting the image property, I would do it in `-awakeFromNib` – staticVoidMan Jun 08 '14 at 08:59
  • btw... I am not totally sure about the specifics of your question so I'm just putting this out there (_although you look like you should be knowing_) but you have to manually drag a `UIImageView` object onto your custom `UITableViewCell`. There is no standard `UIImageView` in a custom UITableViewCell – staticVoidMan Jun 08 '14 at 08:59

2 Answers2

1

You won't get any frame sizes in the -initWithStyle:reuseIdentifier: because the cell hasn't loaded yet which means the subviews haven't been created yet (i.e. are nil) and hence a 0 frame size.
Even for a viewController, you won't get frame size in the -initWithNibName: method.

You would be better off with:

  1. Frame specific related stuff in -layoutSubviews
  2. Setting an initial property in -awakeFromNib (or directly via IB)

Anyways, try this:

//does this method even run? it shouldn't for a custom UITableViewCell
//unless you've mixed up things unnecessarily
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSLog(@"initWithStyle -- %@",self.myImageView);
    }
    return self;
}

//do frame related stuff here
-(void)layoutSubviews
{
    //fyi: this method is called anytime any frame changes

    NSLog(@"layoutSubviews -- %@",self.myImageView);
}

- (void)awakeFromNib
{
    //fyi: this method is called when the view is pulled from the IB

    // Initialization code
    NSLog(@"awakeFromNib -- %@",self.myImageView);

    //do you thang here
    [self.myImageView setImage:[UIImage imageNamed:@"MyImageToUseInThisCell"]];
}

Community
  • 1
  • 1
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
1

Seems that the problem is when dequeuing a custom cell with a reuse identifier and index path the custom cell base class doesn't set those properties. This is not mentioned in the docs which is very unfortunate.

Avba
  • 14,822
  • 20
  • 92
  • 192