1

I am making a subclass of UITableView cell.

I have set the cell contentView, backgroundView, and selectedBackgroundView all to a single CGInsetRect (Of the 180.0 rowHeight set in the tableView). My first problem is when I try to assign the inset rect to the image view- the image gets pushed +-5 points to the right. So I've resorted to making the rect manually.

Then I set the text label size and color, but the specific attributes I assigned do not appear until I click the cell.

When I don't set the text label manually- the image pushes the textLabel offscreen, only showing one letter and an "..." ellipsis.

It seems that every modification I make causes something to break, and it seems like my code is competing with some automatic system. Its very frustrating and I don't know what to do or even how to debug this. Any tips on the gotchas of layoutSubviews in UITableViewCell with storyboards would be awesome, because I seem to be missing something critical.

My layoutSubviews

-(void) layoutSubviews
{
    [super layoutSubviews];

    const CGRect insetRect            = CGRectInset( self.bounds, 7, 4 );

    self.contentView.frame            = insetRect;
    self.backgroundView.frame         = insetRect;
    self.selectedBackgroundView.frame = insetRect;

    self.imageView.frame              = CGRectMake(0, 0, 306, 172);
    self.textLabel.frame = CGRectMake(0, 132, 306, 40);
    self.textLabel.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];

    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1];
}
user
  • 3,388
  • 7
  • 33
  • 67
  • 1
    Why don't you just make the cell with al its subviews in IB? The constraints will be set up there, and you can adjust them as needed. – rdelmar Aug 27 '13 at 02:28
  • I suppose I will. I went for programmatic view construction as a learning lesson, but I've been beaten into submission by all the pitfalls. I still don't get why my layoutSubviews was going nuts though- the storyboard was a stock Master-Detail setup. – user Aug 27 '13 at 02:37
  • Hard to tell without seeing your code. – rdelmar Aug 27 '13 at 02:38
  • @rdelmar Just used storyboard to do everything I've posted above and it turned out how it should. Shame it's so obscure to do things by code though. I feel like I lose control when using IB. – user Aug 27 '13 at 03:11
  • 2
    The reason things are clashing is because you're setting frames. When you use auto layout, you shouldn't be setting any frames, you should be adjusting constraints. – rdelmar Aug 27 '13 at 04:37

0 Answers0