18

I'm using Interface Builder to design a custom UITableViewCell for an iPad application.

I have a dedicated XIB file which has a UITableViewCell as its root view. I am able to change the width of the UITableViewCell to 1024 (in the Size inspector on the right properties pane of XCode) and this DOES resize the cell on the canvas temporarily.

However if afterwards I try to move/resize certain items within the UITableViewCell or close and reopen the XIB, XCode decides to reset the UITableViewCell's width back to 320 (the iPhone specific width).

The cell on the canvas then obscures the items within the cell and makes designing the cell impossible unless I resize the cell again. Once you've gone round this loop a few times it gets rather annoying.

Anyone know how I might set the width so its permanent or some how specify that the UITableViewCell is iPad specific?


To recreate...

  • File > New > File...
  • UserInterface > Empty
  • Device Family == iPad

  • Drag a new UITableViewCell onto the canvas (its created with a width of 320 despite selecting iPad previously)

  • Resize the UITableViewCell's width to 1024 (all good so far)
  • Drag and drop a UILabel onto the left side of the UITableViewCell (still good)
  • Resize the UILabel in any way by dragging a corner (DO NOT USE THE ATTRIBUTES INSPECTOR) - AND... The UITableViewCell's size is reset back to 320

EDIT:

I have just received the following response from Apple to the Radar ticket I created ages ago...

Hello Oliver,

This is a follow-up regarding regarding Bug ID# 13222753.

Engineering has requested the following information in order to further investigate this issue:

We believe this issue has been addressed in the latest Xcode 5.1 Seed.

This is a pre-release version of the Xcode developer toolset for Mac, iPhone, and iPad. It includes the Xcode IDE, iOS Simulator, and all required tools and frameworks for building OS X and iOS apps. Please test with this release, and update your bug report with the results.

Xcode 5.1 Seed - Build 5B71f Posted Date: Dec 13, 2013

I've also just tried to recreate the problem in Xcode 5.0.2 and it looks like its fixed with that build also.

1
  • I reported this too to apple and they say it's closed, but still shows with the latest xcode 5.0.2 and mavericks. This is still an issue. Sometimes it doesn't present when you change fontsizes for labels using the Font inspector and then the mini up-down arrows, but for anything else, it's most likely to fail, aka that the cell changes to 320. This is definitely a bug but don't know if apple wants to solve it, it has ages showing.
    – user3191980
    Jan 13, 2014 at 21:52

4 Answers 4

6

I don't know if you consider this a valid answer, but the problem only seemed to occur for me when I was using autolayout in IB. If I turn that off, the problem of Xcode resizing the UITableViewCell goes away.

To turn off autolayout, just select the UITableViewCell and then on the right in the 'File inspector', there is a checkbox for autolayout to turn it off.

6
  • Interesting, will give this a try when I get to work, thanks! Mar 12, 2013 at 8:10
  • 2
    Accepted, this is certainly only a work-around rather than a solution. The view will resort to the old springs/structs model rather than using the new iOS 6 constraints which is a bit of a bummer. I've tried unchecking "use Autolayout" in order to resize something and then rechecking once I was done but all my previously set constraints are lost. So if you want to use this you need to sacrifice the constraints model. Mar 12, 2013 at 9:00
  • I agree with @HaggleLad, this is not a solution. Neither a workaround. Oct 21, 2013 at 14:30
  • @MartinMoizard. I've never said it it was a solution. I was just posting an answer that might help him workaround the problem if he chose not to use autolayout. When i encountered the problem, i just didn't end up using a nib and created the view programatically so that i could use autolayout Oct 22, 2013 at 2:15
  • I already have Autolayout turned off, still getting this annoying issue :-(
    – Adil Malik
    Oct 19, 2014 at 15:37
5

The workaround is to not add a Table View Cell to your nib. Instead, use a regular View to contain your subviews.

Create a custom UITableViewCell class that loads the nib and sets the top-level nib view as the subview of the cell content view:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        UINib *nib = [UINib nibWithNibName:@"YourNibName" bundle:nil];
        UIView* containerView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
        containerView.translatesAutoresizingMaskIntoConstraints = NO
        [self.contentView addSubview:containerView];

        NSDictionary *views = NSDictionaryOfVariableBindings(containerView);
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[containerView]|" options:0 metrics:nil views:views]];
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView]|" options:0 metrics:nil views:views]];
    }

    return self;
}

Register your class with the table view:

[tableView registerClass:YourCellClass forCellReuseIdentifier:@"YourCellID"]`
2
  • 1
    This is kind of a hack because you lose the properties of a UITableViewCell, such as the backgroundView.
    – Mark
    Oct 29, 2013 at 23:55
  • Works well. Missing containerView.translatesAutoresizingMaskIntoConstraints = NO; Jan 27, 2014 at 9:26
1

I just did a test and had no problems resizing the cell. It did make no difference, if the Xib was created with the 'for iPhone' or 'for iPad' option. So, this works as expected.

The trick is, not to drag the size but, to use the Inspector to change the width - this leaves the cell at it's size of 1024.

3
  • Hi SAE, thanks for your reply, perhaps you created your XIB in a different manner to me? I've added the steps I took to my original post. Feb 14, 2013 at 16:02
  • My workflow is the same - only differs in the way I resize the UILabel. See above.
    – SAE
    Feb 14, 2013 at 16:40
  • Thanks again, OK so you get the same problem as well when dragging? Unfortunately using the inspector isn't really a solution. Guess this is an XCode bug, I'll log a report at bugreport.apple.com Feb 15, 2013 at 8:34
0

I confirm that Edward Huynh's answer is correct. At the same time, I could not have afforded to turn off autolayout, change the height, turn the autolayouts back again. This would require me to redo all autolayout settings for each subview of the content view.

I was just experimenting and found out that opening xib file in TextEdit and changing the width & height of the tableViewCell and the tableViewCellContentView will actually retain all autolayouts and the size of the cell is updated. Few of the constraints' constant values will now be obsolete which can easily be fixed by using "update constraints" on such constraints.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.