4

Is UITableView resizable ? Can I assign a different frame ?

I'm trying this line in a UITableViewController but it doesn't work (on iPhone):

self.tableView.frame = CGRectMake(0, 0, 300, 200);

thanks

0

7 Answers 7

17

Not in a UITableViewController, because in a UITableViewController, self.view == self.tableView.

Try using a UIViewController, implementing the UITableViewDataSource and UITableViewDelegate protocols as necessary.

2
  • Also self.view.frame = CGRectMake(0, 0, 300, 200); doesn't work.
    – aneuryzm
    Aug 23, 2011 at 8:59
  • sure it is: - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; CGRect frame = self.view.superview.bounds; frame.size.width -= 54.0; self.tableView.frame = frame; } Mar 26, 2016 at 6:39
7

You cannot resize the self.tableview.frame Or self.view.frame if you are using UITableViewController But what you can do is

You can set the top margin by using :

UIEdgeInsets inset = UIEdgeInsetsMake(50, 0, 0, 0);
self.tableView.contentInset = inset;

it's Not a good practice , just in case you want more space on top you can use it .

Should not use UITableViewController , Just simply Use UIViewController and Programmatically Create UITableview

0
4

Yes, UITableView is resizeable, but not when you use UITableViewController.

Try adding a UITableView to a normal UIViewController and have it implement the required delegate methods. This way you get a lot more flexibility in customizing your UITableView.

Read the following two links

http://www.skylarcantu.com/blog/2009/09/24/dont-use-uitableviewcontroller-really/

http://flux88.com/2010/03/is-uitableviewcontroller-useless/

4

To expand on Berk's answer:

  1. Create a regular view controller using XCode's template.
  2. Add <UITableViewDelegate, UITableViewDataSource>
  3. Add a @property IBOutlet UITableView, add a UITableView object to the XIB and link them.
  4. Set the delegate and datasource of the UITableView to the class.
  5. Add the following methods:

// # pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  
    NSString *cellIdentifier = @"ServiceCell";
    UITableViewCell *cell = (UITableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] init];
    }
    return [cell autorelease];
}

The result is a UITableViewController with a resizable tableView.

3
  • Is there a way to convert a UITableViewController into a UIViewCOntroller or I have to completely copy all the code ?!? (I'm not using a nib file)
    – aneuryzm
    Aug 23, 2011 at 9:28
  • You can't convert it, but if you create a new UIViewController with or without a NIB you can copy paste all the UITableViewDelegate and UITableViewDatasource methods from your UITableViewController. Note that following the steps above, you are setting the new controller as as delegate and datasource of the internal tableView, so it calls the same methods. The difference with not having a NIB is that you have to do tableView=[[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped] autorelease]; tableView.delegate=self;tableView.dataSource=self; in the viewDidLoad method.
    – Jano
    Aug 23, 2011 at 9:45
  • I've just converted it. Adding few lines. Without NIB is simple.
    – aneuryzm
    Aug 23, 2011 at 9:49
0

You can resize the tableView inside the UITableViewController in methods like

- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView setFrame:CGRectMake(x, y, w, h)];
}

It works also in viewDidAppear ...

0

Try with this:

- (void)viewDidAppear:(BOOL)animated
{   
    // Set your tableView frame in UITableViewController
    [self.tableView setFrame:CGRectMake(0, 320, 320, 480)];
    // To remove the black color space up of the table view and in this case I set it as white
    [[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]];
}
1
  • When I do this, the user can see the change happening. It looks jumpy Jul 15, 2015 at 22:35
0

In order to resize UITableView while using UITableViewController just override viewWillLayoutSubviews like this:

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    CGRect frame = self.view.superview.bounds;
    frame.size.width -= 54.0;
    self.tableView.frame = frame;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.