Search code examples
iosuitableviewresizeretina-display

UITableView to always show 4 cells


I want my UITableView to always show 4 rows, whether it's on retina 3.5 or 4 - with appropriate re-sizing of fonts and subviews in the cell. The app will always be in portrait orientation.

Is there an "elegant" way to do this in storyboard or through constraints?

Or is the best way to determine all the sizes manually, then alter them programmatically?


Solution

  • Set Auto-resize property of UITableView with flexible height, so that it could change height on devices having different screen sizes. Use heightForRowAtIndexPath like this:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return self.yourTableView.frame.size.height/4;
    }
    

    Now, whatever the height of tableview is, you will get same cell heights for all 4 cells.

    Note: For your UITableViewCells set view and elements resize properties like flexible height and flexible positions, so that cells' elements get adjusted according to height changes.