Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell height based on available screen space

I wanted to create a layout for my UITableView such that it's divided into 2 sections: the first section has 3 rows of standard height (44px), and the second section has one row, which contains a UITextView which completely fills up this cell. The tricky part is that I want to size this textView's cell row to the height of the visible tableview area, such that the text view takes up the remaining view on screen. How do I find out, within

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

what the remaining height looks like? I had previously made hard-coded assumptions based on the two iPhone screen sizes available (480 and 568) but with the new iPhone6 and iPhone 6+ screen, I don't want to use any assumed values for calculating this.

like image 771
Z S Avatar asked Nov 06 '25 07:11

Z S


1 Answers

You already have the height of the table view at tableView.bounds.size.height, you just need the height of the first section subtracted from it. so

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

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

With the headers, depending on how they behave, you can use rectForHeaderInSection: or rectForSection: to get the heights

like image 167
Allen Zeng Avatar answered Nov 09 '25 04:11

Allen Zeng