ios 将 UITableView 的高度调整为仅适合总内容大小所需的高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13574071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:20:22  来源:igfitidea点击:

Resize UITableView's height to be as high as it needs to fit only total content size

iosobjective-cuitableviewios5ios6

提问by abisson

So I am having a very basic issue. I have this UITableViewwithin a view, and I would like to make this table's height as high as it needs to be to display ALL the rows within the table. Therefore, I don't want to be able to scroll, I just want all the rows to appear. (The rows are dynamic in quantity and height).

所以我有一个非常基本的问题。我UITableView在 a 中有这个view,我想让这个表的高度尽可能高,以显示表中的所有行。因此,我不想能够滚动,我只想显示所有行。(行的数量和 是动态的height)。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize cellSize = [[[_stepsArray objectAtIndex:indexPath.row]content] sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(528.0f, CGFLOAT_MAX)lineBreakMode:NSLineBreakByWordWrapping];
    return cellSize.height;
}

If there are 20 rows, the table will be very high, but if there is only 1 row, it will be very small and the other content will not appear 19 rows below.

如果有20行,表格会很高,但如果只有1行,就会很小,其他内容不会出现在19行下方。

回答by Edwin Iskandar

if I understand it correctly, you should be able to add up the height for each row and adjust the tableview height based on that:

如果我理解正确,您应该能够将每行的高度相加并基于此调整 tableview 高度:

CGFloat tableHeight = 0.0f;
for (int i = 0; i < [_stepsArray count]; i ++) {
    tableHeight += [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
}
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, tableHeight);

you can do this immediately after [tableView reloadData]

您可以在 [tableView reloadData] 之后立即执行此操作

回答by akelec

Here is more simpler solution: All you need is to set sizeToFiton your UITableView, after it has been populated with cells. If you set delegateand dataSourcethrough xib, you can do this in your viewDidLoadmethod, or viewDidAppear, like this:

下面是更简单的解决方案:所有你需要的是一套sizeToFit对你UITableView,它已经填充了单元格后。如果您设置delegatedataSource通过xib,您可以在您的viewDidLoad方法中执行此操作,或者viewDidAppear,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [yourTableView sizeToFit];
}

But, if you add delegateand dataSourcesomewhere in the code, you have to put sizeToFitafter this:

但是,如果您在代码中的某处添加delegate和,则dataSource必须sizeToFit在此之后放置:

- (void)someMethod
{
    [yourTableView setDelegate:self];
    [yourTableView setDataSource:self];

    [yourTableView sizeToFit];
}

Also, you have to do this after reloadTable, if you do that somewhere.

此外,如果您在某处执行此操作,则必须在 reloadTable 之后执行此操作。

This will resize your table exactly to height which is a sum of its cells height!

这会将您的表格完全调整为高度,即其单元格高度的总和!

回答by Venk

You can try like this,

你可以这样试试

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
        NSLog(@"%f",your_tableView.contentSize.height);
    }
}

In Swift 3.0

Swift 3.0 中

func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == tableView.indexPathsForVisibleRows?.last?.row {
        print("\(tableView.contentSize.height)")
    }
}

回答by Anooj VM

Add an observer for the contentSize property on the table view, and adjust the frame size accordingly

为 table view 上的 contentSize 属性添加一个观察者,并相应地调整框架大小

In viewDidLoad add the following line of code

在 viewDidLoad 中添加以下代码行

[your_tableview addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];

then in the callback:

然后在回调中:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
         CGRect frame = your_tableview.frame;
         frame.size = your_tableview.contentSize;
         your_tableview.frame = frame;
    }

Hope this will help you.

希望这会帮助你。

回答by phatmann

Set a height constraint on the table, connect to an outlet, and add the following to your view controller:

在桌子上设置高度约束,连接到插座,并将以下内容添加到您的视图控制器中:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    tableViewHeightContraint.constant = tableView.contentSize.height
}

回答by Chauyan

Simply use tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];That will solve your problem.

只需使用tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];That 将解决您的问题。