2

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.

4

5 回答 5

11

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?

于 2012-08-17T09:49:19.103 回答
5

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

于 2013-08-25T09:10:06.027 回答
4

I had the same issue, just needed to move the code from

   - (void) viewDidLoad

to

   - (void)viewDidAppear:(BOOL)animated 
于 2014-02-17T02:46:33.773 回答
2

This is tricky, its hard to resize things like this dynamically.

i would try "setNeedsLayout" And "setNeedsDisplay" for your table, and your screen to force a redraw.

Other than that, I would storyboard it to the minimum size and use code to expand it.

Generally apple doesn't like us doing this, your buttons should be drawn over the top of the view inside another view, if thats possible.

Sorry I can't be more precise but I have solved all these issues by mucking around and with hacks, and giving up on resizing things and doing re-designs. Please let me know how you go though :)

于 2012-08-17T09:37:27.100 回答
1

Make sure you are setting the frame after the table has loaded.

Which method do you call that code in?

于 2012-08-17T09:38:25.430 回答