Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - resize UITableView height programmatically

I'm having a problem trying to programmatically resize the height of a UITableView hosted within a UIViewController, using iOS5 and Storyboards. The VC displays a master/detail record style with the UITableView displaying the detail records. Depending on the type of master record shown, a set of buttons may be needed at the foot of the screen. If the buttons are not needed then I want the UITableView to extend it's height to take advantage of the space available. I'm using the following code :

CGRect tableFrame = [tableListView frame];
if (blnApprovalRec == YES)
    tableFrame.size.height = 127;
else
    tableFrame.size.height = 170;
[tableListView setFrame:tableFrame];

This code is called whenever the master record changes, including when the screen first loads in viewDidLoad. The problem is that when the VC loads, the UITableView doesn't paint using the size specified - it just paints with the default size from IB. Once the user changes the master record so the table is reloaded then everything works fine and the size changes as required. I've tried forcing a repaint using setNeedsDisplay, setNeedsLayout and reloadData but none of these worked.

like image 721
Jonathan Wareham Avatar asked Nov 18 '25 01:11

Jonathan Wareham


2 Answers

The problem is that when the VC loads, the UITableView doesn't paint using the size specified

This happens, when table view is loaded, but it's UI is not getting refreshed. Please verify if you have forcefully called in viewDidLoad or viewWillAppear.

Hopefully your this code is in seperate method:

CGRect tableFrame = [tableListView frame];
if (blnApprovalRec == YES)
   tableFrame.size.height = 127; 
else
   tableFrame.size.height = 170; 
[tableListView setFrame:tableFrame];

When the view appear initially, you may have the default selected value from master record. You can set that value/instance in calling function in viewWillAppear.

Can you show method name and code for, how you are calling above snippets of code forcefully?

like image 90
Ankit Thakur Avatar answered Nov 20 '25 16:11

Ankit Thakur


You can resize UITableView programmatically but you need to create UITableView programmatically too. Don't use Storyboard.

like image 34
Jesdaloha Multimedia Avatar answered Nov 20 '25 18:11

Jesdaloha Multimedia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!