Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize UITableView's height to be as high as it needs to fit only total content size

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.

like image 733
abisson Avatar asked Nov 26 '12 22:11

abisson


4 Answers

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:

CGFloat tableHeight = 0.0f;
for (int i = 0; i < [_stepsArray count]; i ++) {
    tableHeight += [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
}
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, tableHeight);

you can do this immediately after [tableView reloadData]

like image 126
Edwin Iskandar Avatar answered Nov 03 '22 07:11

Edwin Iskandar


Here is more simpler solution: All you need is to set sizeToFit on your UITableView, after it has been populated with cells. If you set delegate and dataSource through xib, you can do this in your viewDidLoad method, or viewDidAppear, like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [yourTableView sizeToFit];
}

But, if you add delegate and dataSource somewhere in the code, you have to put sizeToFit after this:

- (void)someMethod
{
    [yourTableView setDelegate:self];
    [yourTableView setDataSource:self];

    [yourTableView sizeToFit];
}

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!

like image 28
akelec Avatar answered Nov 03 '22 08:11

akelec


You can try like this,

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
        NSLog(@"%f",your_tableView.contentSize.height);
    }
}

In Swift 3.0

func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == tableView.indexPathsForVisibleRows?.last?.row {
        print("\(tableView.contentSize.height)")
    }
}
like image 4
Venk Avatar answered Nov 03 '22 08:11

Venk


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

[your_tableview addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];

then in the callback:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
         CGRect frame = your_tableview.frame;
         frame.size = your_tableview.contentSize;
         your_tableview.frame = frame;
    }

Hope this will help you.

like image 3
Anooj VM Avatar answered Nov 03 '22 09:11

Anooj VM