3

I have a view controller that contains a table view (see screenshot below). This table view always has exactly 3 rows, and these rows expand when clicked to show more information. How can I make it so that the table view's height always shows exactly these three rows, and no more? As you can see in the screenshot, empty rows with no content are being shown.

Note that the expanded rows may contain views other than UILabels.

Here's how it looks in IB:

enter image description here

4

6 回答 6

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);
}
于 2012-10-10T17:34:29.253 回答
0

如果垂直空间大于显示实际单元格所需的空间,UITableView 会插入假的空单元格。您无法真正控制这种行为,您最多只能将表格视图的高度设置为足够小。

于 2012-10-10T14:30:24.143 回答
0

使用frame您想通过编程操作的任何控件的属性。frame还有size和的两个性质origin。使用该size属性,您可以操作宽度和高度,并使用origin您可以操作控件xy属性。您想要的可以通过以下方式完成:

CGRect frame = control.frame; //control is your UIControl

frame.size.height = 10.0; //suppose 10 is the desired height.

control.frame = frame;

希望这是您正在寻找的。

于 2012-10-10T14:34:51.627 回答
0

尝试初始化表格的页脚视图:

TableView.tableFooterView = [[UIView alloc] init];

于 2012-10-10T14:42:03.850 回答
0

添加此 footerview 方法,这将起作用。这将删除空单元格。

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, 0)];
v.backgroundColor = [UIColor clearColor];
[tableView setTableHeaderView:v];
[tableView setTableFooterView:v];
return v;
}
于 2012-10-10T14:52:07.227 回答
0

在我希望表格与内容大小完全一致的情况下,这两行对我有用。这是一种“幻觉”,但就像一种魅力。

 self.tableView.tableFooterView = [UIView new];
 self.view.backgroundColor = [UIColor whiteColor];
于 2015-09-09T06:22:37.863 回答