13

I have some code that creates a table cell with a slider. It's pretty straightforward and it sizes well on the iPhone. I've anonymized it a bit here:

UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foo"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CGRect contentViewFrame = cell.contentView.frame;
CGRect sliderFrame = CGRectMake(10, 0, 280, contentViewFrame.size.height);
UISlider* slider = [[UISlider alloc] initWithFrame:sliderFrame];
UIImage* minimumImage = [UIImage imageNamed:@"min.png"];
UIImage* maximumImage = [UIImage imageNamed:@"max.png"];
slider.minimumValueImage = minimumImage;
slider.maximumValueImage = maximumImage;
slider.value = 0.5f;
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:slider];
[slider release];

Of course, this is incorrectly sized for the iPad. So my first thought was to set the autoresizingMask property to UIViewAutoresizingFlexibleWidth. Problem solved, right? Nope. Now on the iPhone, the width of the slider-plus-images content is less than 280 and so it doesn't go right to the end -- it ends up about 20 pixels short.

On the iPad, the same thing -- the width of the UISlider automatically resizes to about 20 pixels short of the end of the cell.

Perhaps the auto resize flag is paying attention to the non-existent accessoryView of the cell? I tried setting it to nil explicitly, but I think it's nil by default, so nothing changed.

I'd like this cell's content to resize automatically to be the "full" width of the cell, regardless of device and orientation. Is there an easy way to do this?

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128

4 Answers4

10

It works exactly how you described. I am inclined to think it's iOS bug. On iPAD when you create new UITableViewCell its width set for 320. hardcoded(!) both view and contentView. It does not resize properly if set to UIViewAutoresizingFlexibleWidth. I had it set to view.frame.size.width/2 with funny results: on iPhone it's 160, on iPad it's 608!!!

I ended up manually resizing my cells and their content.

bioffe
  • 6,283
  • 3
  • 50
  • 65
6

Bit late but i found the solution of the same question today, but you need to create a custom UITableViewCell. Then you can overwrite the function

- (void) layoutSubviews
{
    [dateLabel setFrame:CGRectMake(10.f, 16.f, 80.f, 12.f)];
    [textLabel setFrame:CGRectMake(106.f, 16.f, contentView.frame.size.width-105.f + 1.f, 12.f)];
}

In that function the self.frame.size.width is the actual one.
And it works with rotation of the device, too.

Seega
  • 3,001
  • 2
  • 34
  • 48
  • You don't need to manually set those subviews frames. You just need to call `[super layoutSubviews];` in there, that will make subviews autoresize correctly. – Hlung Jun 21 '12 at 11:15
  • 1
    You do, if you have custom subviews (in your custom cell) – Authman Apatira Aug 11 '12 at 01:02
  • 1
    on layoutSubviews, docs state "You should not call this method directly." Instead call setNeedsLayout – Max MacLeod Dec 07 '12 at 12:01
  • 1
    +1 for overriding layoutSubviews, custom subviews, and not calling the method directly. Additionally, when using the "grouped" display for the table, you should be using `contentView.frame` instead of `self.frame`, as `self.frame` appears to return the width of the table, and in grouped mode the cells are narrower than the table. (pardon my ObjC, I use MonoTouch and C#, so the calls are different. concepts are the same). – cod3monk3y Feb 03 '13 at 05:27
  • 1
    As @cod3monk3y said--when using cells, you should put your content in `contentView`, not the cell itself. Resize based on the bounds of `contentView`. – nielsbot Oct 18 '13 at 18:41
3

Set your cell's contentMode to UIViewContentModeRedraw.

Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
3

You should be able to tell the resizing system to "stick" the object a fixed distance from the right edge (where it's not resizing far enough). If you experiment with IB you can create a view that resizes in width and is fixed to the right side.

Do you have UIViewAutoresizingFlexibleRightMargin set as well?

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • I don't have `UIViewAutoresizingFlexibleRightMargin` set. Can you explain a bit more how to do this programatically? The code posted above is anonymized but is exactly what I have now. – Shaggy Frog Jul 29 '10 at 06:56