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
Not in a UITableViewController, because in a UITableViewController, self.view == self.tableView
.
Try using a UIViewController, implementing the UITableViewDataSource and UITableViewDelegate protocols as necessary.
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
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/
To expand on Berk's answer:
<UITableViewDelegate, UITableViewDataSource>
@property IBOutlet UITableView
, add a UITableView
object to the XIB and link them. UITableView
to the class. // # 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.
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 ...
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]];
}
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;
}