GtDriver
GtDriver

Reputation: 65

Xcode - Blank Space After UITableView

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): enter image description here

This is I see at the bottom of the screen on an iPhone 6 simulator: enter image description here This is what I see at the bottom of the screen on an iPhone 4s simulator: enter image description here

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

Answers (1)

Jose
Jose

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:

  1. The table view is fixed in portrait mode
  2. The table view has only one section

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

Related Questions