iOS:如何动态设置UITableView的高度

Jam*_*rpe 3 iphone uitableview ios

我有一个包含表视图的视图控制器(见下面的截图).此表视图始终只有3行,单击这些行时会展开以显示更多信息.我怎样才能使表格视图的高度始终显示这三行,而不是更多?正如您在屏幕截图中看到的那样,显示没有内容的空行.

请注意,展开的行可能包含UILabel以外的视图.

以下是它在IB中的表现:

在此输入图像描述

Adi*_*ane 5

我想你需要这样的东西

//Return number of rows in section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [self setTableHeight];
    return 3;
}

//Method which lets you calculate the height of tableView depending on your 3 cells
- (void)setTableHeight
{

    if(_isRowExpanded == NO)
    {
        tableHeightCalculated = rowHeight * 3; // rowHeight use default 44 or ur custom
    }
    else
    {
        //You need to put some logic here to Calculate or put approx height of  Expanded_Row_Height
        tableHeightCalculated = rowHeight * 2 + Expanded_Row_Height;

        if(tableHeightCalculated > maxHeightAllowed) //maxHeightAllowed is your current table height in snapshot
        {
            tableHeightCalculated = maxHeightAllowed;
        }
    }
    tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y,
                                 tableView.frame.size.width,  tableHeightCalculated);
}
Run Code Online (Sandbox Code Playgroud)