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
.
This answer is courtesy of @BenLings from one of the comments above. It's a really clean solution and worth having its own answer:
- (CGSize)contentSizeForViewInPopover {
// Currently no way to obtain the width dynamically before viewWillAppear.
CGFloat width = 200.0;
CGRect rect = [self.tableView rectForSection:[self.tableView numberOfSections] - 1];
CGFloat height = CGRectGetMaxY(rect);
return (CGSize){width, height};
}
I wanted to change the contentSizeForViewInPopover
when my view appeared to match the UITableView
and also when I call reloadData
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
-(void) reloadViewHeight
{
float currentTotal = 0;
//Need to total each section
for (int i = 0; i < [self.tableView numberOfSections]; i++)
{
CGRect sectionRect = [self.tableView rectForSection:i];
currentTotal += sectionRect.size.height;
}
//Set the contentSizeForViewInPopover
self.contentSizeForViewInPopover = CGSizeMake(self.tableView.frame.size.width, currentTotal);
}
Indirect Approach :
Set your custom height for your UITableViewCell using
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row] * 40;
}
Find the total number of rows at a point of your program ... Using the numberOfRowsInSection, get the number of rows.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.yourArray count];
//yourArray is the array with which you are populating the tableView
}
If you have more than one section, multiple it with the result for the number of rows for the effective number of rows.
Now
UITableView Actual Height = Height of UITableViewCell * Total number of rows.
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
I did it like this from controller containing a table:
- (void)updatePopoverContentSize {
CGSize size = { CGFLOAT_MAX, CGFLOAT_MAX };
size = [self.tableView sizeThatFits: size];
size.width = 320; // some hard-coded value table doesn't return anything useful for width
size.hight = MIN(size.hight, 400); // make sure it is not too big.
self.contentSizeForViewInPopover = size;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With