How can I adjust the UITableView
height dynamically? Let's say I have 3 rows and each row has a height of 65. When a new row is added to the UITableView
i want it to adjust it's height dynamically to fit the new row. And vice versa when a row is deleted to shrink.
-
1Did you try anything yet? – Raptor Apr 23 '14 at 09:53
-
I tried this: http://stackoverflow.com/a/14228015/1129959 but it's too complicated and not obvious at all – Legnus Apr 23 '14 at 09:55
-
Show your codes please. – Raptor Apr 23 '14 at 09:55
-
@ŁukaszTomaszewski not a duplicate. cell height vs table height – Raptor Apr 23 '14 at 09:58
-
@Legnus Will you consider to have `UITableView` occupy the full height of the page, instead of dynamically changing the height? This is the most common usage. and this question is unrelated to [tag:xcode5] – Raptor Apr 23 '14 at 09:58
-
3All are in hurry to give answer without reading whole question. – ChintaN -Maddy- Ramani Apr 23 '14 at 09:59
-
Not a duplicate, the other is talking about UITableViewCell height, I'm talking about UITableView height. and @Raptor I do not have any code yet to show. just a simple tableview that i want it to have a dynamic height for the number of rows – Legnus Apr 23 '14 at 09:59
-
@Raptor I can't do that since it happens to be at the end of a UIScrollView, and i need to adjust the ScrollView height based on the UITableView height – Legnus Apr 23 '14 at 10:02
-
Interesting. `UITableView` itself contains a scrollview. Double scrolling is not suggested in User Experience. – Raptor Apr 23 '14 at 10:04
-
@Raptor The user is not allowed to scroll in my tableview since i disabled the scrolling – Legnus Apr 23 '14 at 10:05
-
Weird structure, but if you insist, that's okay. I suggest to use a simple `UITableView` only – Raptor Apr 23 '14 at 10:07
3 Answers
Edit : Misread
The thing depends on the (height of table cell row * number of rows in table) as the tableview.frame.size.height.
Well I would suggest something like this..
-(void)correctHeight
{
//Note : if all cells are having common value
CGFloat heightOfCell=[table rowHeight];
CGFloat expectedHeightOfTable=0;
for (int i=0; i< [table numberOfSections]; i++) {
//header section
CGFloat heightOfHeaderView=[table headerViewForSection:i].frame.size.height;
expectedHeightOfTable = expectedHeightOfTable +heightOfHeaderView;
//content section
expectedHeightOfTable = expectedHeightOfTable + heightOfCell*[table numberOfRowsInSection:i];
}
[table setFrame:CGRectMake(table.frame.origin.x, table.frame.origin.y, table.frame.size.width,expectedHeightOfTable)];
}
This will consider header and each row content and all value populated prormatically..So dont need to add some hardcoded values

- 19,955
- 12
- 56
- 101
-
-
Thank you i tried it, the height of the tableview is changing while debugging but visually it's always the same height – Legnus Apr 23 '14 at 10:36
-
The tableview height can be set by,
[tableView setFrame:CGRectMake(0,0,width,height)];
But for example if u set the height as 150 and you 5 rows of height 60px using tableView:heightForRowAtIndexPath: then the tableView will become scrollable as UITableView is a sub class of UIScrollView.

- 651
- 5
- 13
As said in comment, Weird structure, but if you insist, that's okay. I suggest to use a simple UITableView
only.
Assume you have IBOutlet
to UITableView
and UIScrollView
...
Before adding codes in viewWillAppear
, add:
float cellHeight = 65.0f;
int numberOfCells = 3;
In - (void)viewWillAppear:animated
, add the following after [super viewWillAppear:animated]
:
// remember fill the table with data using UITableViewDataSource's functions first
// refresh UITableView data
[tableView reloadData];
// resize UITableView
[tableView setFrame:CGRectMake(0, 0, tableView.frame.size.width, cellHeight * numberOfCells)];
// make UIScrollView the same size as UITableView
[scrollView setContentSize:tableView.frame.size];
[scrollView setNeedsDisplay];
// remember to lock tableView's scrolling, either in code / Interface Builder
[tableView setScrollEnabled:NO];
And in UITableViewDelegate
's delegate methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return numberOfCells;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cellHeight;
}
Last, if you wish to double verify if any of the table cell is out of sight, you can use:
- (NSArray *)visibleCells

- 53,206
- 45
- 230
- 366
-
-
Yes, if header is set visible (since header is hidden in most cases). Really depends on OP's layout. – Raptor Apr 23 '14 at 10:23
-
Thank U i tried it, the height of the tableview is changing while debugging but visually it's always the same height – Legnus Apr 23 '14 at 10:29
-
My bad. After looking up documentation, the codes should be put inside `viewWillAppear:animated` instead of `viewDidLoad` in order to make dynamic changes on table view size – Raptor Apr 24 '14 at 02:48