Reputation: 65
What is the best/correct way to get a UITV Cells to resize based on the display they're being viewed on? Basically, I would like the same number of rows displayed on screen, regardless of the device being used. Is this done in code or am i missing something in the storyboard?
Please see my example below (this is what i see in storyboard):
This is I see at the bottom of the screen on an iPhone 6 simulator: This is what I see at the bottom of the screen on an iPhone 4s simulator:
I'm pretty new to Xcode so appreciate this is probably a well documented question... but i've been at it for a while now and i'm at the point where i need a little help -- so any help appreciated.
Thanks in advance.
Upvotes: 0
Views: 61
Reputation: 106
You will have to handle this in your UITableViewController
subclass, there isn't a way to do what you want to do in the storyboard.
Assuming the following:
Adding this method to your UITableViewController
subclass will return a dynamic height based on the height of the table view:
(Swift)
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
let numCells = self.tableView(self.tableView, numberOfRowsInSection:0)
let cellHeight = self.tableView.frame.size.height / CGFloat(numCells)
return cellHeight
}
(Objective-C)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger numCells = [self tableView:self.tableView numberOfRowsInSection:0];
CGFloat cellHeight = self.tableView.frame.size.height / numCells;
return cellHeight;
}
Upvotes: 1