首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS:如何动态设置UITableView的高度

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

Stack Overflow用户
提问于 2012-10-10 22:27:11
回答 6查看 7.1K关注 0票数 3

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

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

这是它在IB中的外观:

EN

回答 6

Stack Overflow用户

发布于 2012-10-11 01:34:29

我想你需要这样的东西

代码语言:javascript
复制
//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);
}
票数 5
EN

Stack Overflow用户

发布于 2012-10-10 22:30:24

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

票数 0
EN

Stack Overflow用户

发布于 2012-10-10 22:34:52

使用要通过编程操作的任何控件的frame属性。frame还具有sizeorigin的两个属性。使用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;

希望这就是你要找的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12821804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档