Height of UITableViewCell based on available screen space

I wanted to create a layout for mine UITableView

so that it is split into 2 sections: the first section has 3 lines of standard height (44px), and the second section has one line containing UITextView

that completely fills that cell. The tricky part is that I want the size of this line to textView

be equal to the height of the viewable area tableview

so that the text view takes up the rest of the view on the screen. How to find out within

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

what does the remaining height look like? I have previously made hard-wired guesses based on two iPhone screen sizes (480 and 568), but with the new iPhone6 ​​and iPhone 6+ screen, I don't want to use any guesses to calculate this.

+3


source to share


2 answers


You already have the height of the table view in tableView.bounds.size.height

, you just need the height of the first section to subtract from it. So

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 44;
    }

    return tableView.bounds.size.height - 44 * 3;
}

      



With headers, depending on how they behave, you can use rectForHeaderInSection:

or rectForSection:

to get the height

+3


source


If you are sure that you will never change the number of lines, you can make it static from the storyboard. You can insert as many cells as you like. And when auto-detecting or auto-socializing, you can set the cells to be static or grow with screen size



But if you really want to do it programmatically, you get the screen height by using self.view.bounds.size.height

undo 3 cells and 2 section headers, and the rest with a UITextView cell.

0


source







All Articles