1
votes

I am currently working with a custom tableviewcell xib and want the size of it to be automatically resized to the tableview's width and it's row's height.

The size of the cell's view does not auto adjusts to the tableview. So I have tried to change the frame inside the tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) delegate no changes are reflected (if I print the frame size before and after it does in fact change.)

Does anyone know a solution for this? The best case would be to make the cell's frame auto adjust to the tableview's width and row height.

Edit: After further examination the problem probably has to do with iOS8 Size classes. When I resize the cell inside the xib, it will just expand to a greater width than the table view's size. The label is supposed to be in the center (had set them with constraints and programmatically, got the same results).

Here's a screenshot:

enter image description here

2
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath - memmons
What do you mean by, it doesn't adjust? Is the cell not the full width of the table view? - rdelmar
@rdelmar Yes, the cell will keep the width defined in the xib, either if it's larger or smaller. - Deivuh
Hmmm... I've never seen this when I've use a xib based cell. I just checked out one of my projects, and no matter what size I make the cell in the xib, it always resizes to the width of the table. I think you need to provide more information about what you did in your xib. - rdelmar
@rdelmar Nevermind, it was my mistake. Had a tableview inside the viewcontroller, I never set constraints nor defined the tableview to adjust, so it kept the base size of the view controller inside the storyboard, that is why the labels never seem to center. Thanks for your help - Deivuh

2 Answers

6
votes

Found the problem, and it was my mistake. I never set constraints to the TableView inside the ViewController, so it maintained the default base size defined by Size Classes introduced in iOS 8.

That is why the width of the cell expanded about the double of the view controller's width.

0
votes

What i understand from your Question is You want to set cell's width equal to table's width And cell's height equal to tableviewCell 's height!

By default The height of tableviewCell is 44. So you can set Cell's Height to 44! and you get tableview's Width by

self.tableview.frame.size.width

and You can set cell's Frame by cell.frame!

for Example

CGRect cellRect = CGRectMake(0, 0, self.tableview.frame.size.width ,44);
cell.frame = cellRect;

Hope all things Going Well and that may help you!