开发者

UITableView with different row heights for different sections

开发者 https://www.devze.com 2023-02-21 06:32 出处:网络
I am using custom cells for different sections within my tableview. I know that I can adjust the height of the roows of my table in Interface Builder.

I am using custom cells for different sections within my tableview. I know that I can adjust the height of the roows of my table in Interface Builder. but how do I control the height of my tableview's rows if I have 3 sections each with a different row开发者_如何学Python height?

Many thanks


Implement tableView:heightForRowAtIndexPath: in your UITableViewDelegate (which is most likely your table-view's controller).


In your heightForRowAtIndexPath method set the section with this indexPath.section. It set the section index where you want to set different row height for a particular cell compare to others. And inside it set the indexPath.row It set the row index where you actually set the different height.

In the below example I set 80.0f height for the 1st cell of my 4th section. And others set as 50.0f.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3)
    {
        if (indexPath.row == 0)
        {
            return 80;
        }
    }
    else
    {
        return 50;
    }
    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消