bahadir arslan
bahadir arslan

Reputation: 4762

UITableView Dynamic Height

I have got a UITableView in my ViewController and it's row has custom content. Every cell's height dynamically calculated according to cell's content with - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath method.

But how can set UITableView's height? Because if my table contains only one row, after row i see a big whitespace in my table? Isn't there any property to set table's height dynamically increase or reduce according to its contents?

Upvotes: 1

Views: 2736

Answers (2)

Toseef Khilji
Toseef Khilji

Reputation: 17409

Try the contentSize method, which is inherited from UITableView’s superclass, UIScrollView. However, you may find that contentSize returns an incorrect or out of date value, so you should probably call layoutIfNeeded first to recalculate the table’s layout.

[tableView layoutIfNeeded];

// Allows you to perform layout before the drawing cycle happens. //-layoutIfNeeded forces layout early. So it will correctly return the size. Like dreaming before doing.

CGSize tableSize=tableView.contentSize;

Upvotes: 1

wattson12
wattson12

Reputation: 11174

The contentSize will be adjusted as needed by the number of rows and sections you have, if you want to change the actual size of the table view you should adjust the frame property

you might just be better off putting a backgroundView or colour on the table if its that the white background when you only have a few rows is bothering you

Upvotes: 4

Related Questions