I have an ipad popover that contains an UITableView
. After the table is populated, it usually has a few items in it only (4-5), so I'm looking for a way to resize the popover (contentSizeForViewInPopover
) to the actual table height (the summed height of all its cells).
So, I do have the height but I'm not sure where to call contentSizeForViewInPopover
, I did try to call it in: viewDidAppear
and viewWillAppear
but with no success since it seems that the table gets populated later and the actual height is only available later.
Any thoughts on this? Thanks!
EDIT: My cells have different heights based on the content they carry, I can't pre-calculate the height with noOfRows * cellHeight
.
Indirect Approach :
Set your custom height for your UITableViewCell using
Find the total number of rows at a point of your program ... Using the numberOfRowsInSection, get the number of rows.
If you have more than one section, multiple it with the result for the number of rows for the effective number of rows.
Now
Updated Answer:
If the cell sizes vary, you might have to do do one of these:
Force the text to a smaller size so that all cells have equal height... This can be done using
'sizeToFit'
You will have to find the height of the text using another function. Something like...
You can look at THIS tutorial for resizing UITableViewCell for variable text.
https://discussions.apple.com/thread/1525150?start=0&tstart=0
An alternate way to get the height is to add the
UITableViewController
's view with analpha
set to0.0
to the view hierarchy and then get the content size using thecontentSize
property of the table view. This is possible as the table view is a subclass of the scroll view.Once you have the content size, set the value of
contentSizeForViewInPopover
and then push it onto the popover.I wanted to change the
contentSizeForViewInPopover
when my view appeared to match theUITableView
and also when I callreloadData
as I only call this when removing or adding rows or sections.The following method calculates the correct height and width and sets the
contentSizeForViewInPopover
There's no need to create an artificial hook, this works well (on iOS 7 at least):
This should do the trick
I did it like this from controller containing a table: