So I am having a very basic issue. I have this UITableView
within a view
, and I would like to make this table's height as high as it needs to be to display ALL the rows within the table. Therefore, I don't want to be able to scroll, I just want all the rows to appear. (The rows are dynamic in quantity and height
).
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize cellSize = [[[_stepsArray objectAtIndex:indexPath.row]content] sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(528.0f, CGFLOAT_MAX)lineBreakMode:NSLineBreakByWordWrapping];
return cellSize.height;
}
If there are 20 rows, the table will be very high, but if there is only 1 row, it will be very small and the other content will not appear 19 rows below.
Simply use
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
That will solve your problem.Here is more simpler solution: All you need is to set
sizeToFit
on yourUITableView
, after it has been populated with cells. If you setdelegate
anddataSource
throughxib
, you can do this in yourviewDidLoad
method, orviewDidAppear
, like this:But, if you add
delegate
anddataSource
somewhere in the code, you have to putsizeToFit
after this:Also, you have to do this after reloadTable, if you do that somewhere.
This will resize your table exactly to height which is a sum of its cells height!
Add an observer for the contentSize property on the table view, and adjust the frame size accordingly
In viewDidLoad add the following line of code
then in the callback:
Hope this will help you.
if I understand it correctly, you should be able to add up the height for each row and adjust the tableview height based on that:
you can do this immediately after [tableView reloadData]
Set a height constraint on the table, connect to an outlet, and add the following to your view controller:
You can try like this,
In Swift 3.0