动态更改UITableView高度 [英] Change UITableView height dynamically

查看:160
本文介绍了动态更改UITableView高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于其单元格高度的总和,从另一个viewcontroller更改tableview的高度,因为它们是动态的。它可能吗?谢谢

I want to change the height of my tableview from another viewcontroller based on the sum of its cells' heights, as they are dynamic. Is it at all possible? Thanks

附加组件:

我基本上拥有的是一个UserProfileViewController,其上有一半的容器视图屏幕。在那里我添加了不同的其他视图控制器:

What I basically have is a UserProfileViewController that has a containerview on half of the screen. There I add different other viewcontrollers:

如果是墙按钮,这就是我添加viewcontroller的方式,以及后续的tableview:

In the case of the wall button this is how I add the viewcontroller and it's subsequent tableview:

- (IBAction)wallButtonPressed:(id)sender
{
    //Check if there is an instance of the viewcontroller we want to display. If not make one and set it's tableview frame to the container's view bounds
    if(!_userWallViewController) {
        self.userWallViewController = [[WallViewController alloc] init];
//        self.userWallViewController.activityFeedTableView.frame = self.containerView.bounds;

    }

    [self.userWallViewController.containerView addSubview:self.userWallViewController.activityFeedTableView];
    //If the currentviewcontroller adn it's view are already added to the hierarchy remove them
    [self.currentViewController.view removeFromSuperview];
    [self.currentViewController removeFromParentViewController];

    //Add the desired viewcontroller to the currentviewcontroller
    self.currentViewController = self.userWallViewController;

    //Pass the data needed for the desired viewcontroller to it's instances
    self.userWallViewController.searchURLString = [NSString stringWithFormat:@"event/user/%@/", self.userID];
    self.userWallViewController.sendCommentURLString = [NSString stringWithFormat:@"event/message/%@", self.userID];

    [self.userWallViewController.activityFeedTableView reloadData];

    self.userWallViewController.totalCellHeight = ^(float totalCellHeight){

        self.scrollView.contentSize = CGSizeMake(320.0, totalCellHeight);
        CGRect newFrame = self.userWallViewController.containerView.frame;
        newFrame.size.height = totalCellHeight + 33.0;
        self.userWallViewController.containerView.frame = newFrame;

        self.userWallViewController.activityFeedTableView.frame = self.containerView.bounds;
    };

    //Add this containerview to the desired viewcontroller's containerView
    self.userWallViewController.containerView = self.containerView;


    //Add the needed viewcontroller and view to the parent viewcontroller and the containerview
    [self addChildViewController:self.userWallViewController];
    [self.containerView addSubview:self.userWallViewController.view];

    //CLEAN UP THE CONTAINER VIEW BY REMOVING THE PREVIOUS ADDED TABLE VIEWS
    [self.userFansViewController.userSimpleTableView removeFromSuperview];
    [self.fanOfViewController.userSimpleTableView removeFromSuperview];
    [self.userPublishedMovellaListViewController.gridView removeFromSuperview];

    [self.userPublishedMovellaListViewController removeFromParentViewController];

    self.userPublishedMovellaListViewController = nil;
}

在那个viewcontroller中我初始化我的tableview:

and in that viewcontroller this is where I initialize my tableview:

-(UITableView *)activityFeedTableView
{
    if (!_activityFeedTableView) {
        _activityFeedTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 850.0) style:UITableViewStylePlain];
    }
    return _activityFeedTableView;
}

我正在计算单元格高度的总和,问题是在调用te tableview的getter之后调用cell的height方法。因此,我需要某种方式来了解细胞的高度方法何时为所有细胞完成,然后我可以调整我的tableview。谢谢

I am calculating he total sum of the cell's height, the problem is that the cell's height method is called way after the getter of te tableview is called. So I would need some sort of way to know when the cells' height method is done for all cells and then I can resize my tableview. Thanks

推荐答案

没有系统功能可以根据tableview的内容更改表的高度。话虽如此,可以根据内容以编程方式更改tableview的高度,特别是基于tableview的 contentSize (比手动计算高度更容易)你自己)。一些细节取决于您是否使用iOS 6中的新自动布局。

There isn't a system feature to change the height of the table based upon the contents of the tableview. Having said that, it is possible to programmatically change the height of the tableview based upon the contents, specifically based upon the contentSize of the tableview (which is easier than manually calculating the height yourself). A few of the particulars vary depending upon whether you're using the new autolayout that's part of iOS 6, or not.

但假设您正在配置表视图的基础 viewDidLoad 中的模型,如果你想调整tableview的高度,你可以在 viewDidAppear 中执行此操作:

But assuming you're configuring your table view's underlying model in viewDidLoad, if you want to then adjust the height of the tableview, you can do this in viewDidAppear:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self adjustHeightOfTableview];
}

同样,如果你曾经执行 reloadData (或以其他方式添加或删除行),你要确保你也手动调用 adjustHeightOfTableView ,例如:

Likewise, if you ever perform a reloadData (or otherwise add or remove rows) for a tableview, you'd want to make sure that you also manually call adjustHeightOfTableView there, too, e.g.:

- (IBAction)onPressButton:(id)sender
{
    [self buildModel];
    [self.tableView reloadData];

    [self adjustHeightOfTableview];
}

所以问题是我们的 adjustHeightOfTableview应该是什么做。不幸的是,这是您是否使用iOS 6自动布局的功能。您可以通过打开故事板或NIB来确定是否已打开自动布局,然后转到文件检查器(例如,按选项 + 命令 + 1 或点击右侧面板上的第一个标签页:

So the question is what should our adjustHeightOfTableview do. Unfortunately, this is a function of whether you use the iOS 6 autolayout or not. You can determine if you have autolayout turned on by opening your storyboard or NIB and go to the "File Inspector" (e.g. press option+command+1 or click on that first tab on the panel on the right):

让我们假设自动布局关闭了一秒钟。在这种情况下,它非常简单, adjustHeightOfTableview 只会调整tableview的框架

Let's assume for a second that autolayout was off. In that case, it's quite simple and adjustHeightOfTableview would just adjust the frame of the tableview:

- (void)adjustHeightOfTableview
{
    CGFloat height = self.tableView.contentSize.height;
    CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;

    // if the height of the content is greater than the maxHeight of
    // total space on the screen, limit the height to the size of the
    // superview.

    if (height > maxHeight)
        height = maxHeight;

    // now set the frame accordingly

    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.tableView.frame;
        frame.size.height = height;
        self.tableView.frame = frame;

        // if you have other controls that should be resized/moved to accommodate
        // the resized tableview, do that here, too
    }];
}

如果您的自动布局已开启, adjustHeightOfTableview 会调整你的tableview的高度约束:

If your autolayout was on, though, adjustHeightOfTableview would adjust a height constraint for your tableview:

- (void)adjustHeightOfTableview
{
    CGFloat height = self.tableView.contentSize.height;
    CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;

    // if the height of the content is greater than the maxHeight of
    // total space on the screen, limit the height to the size of the
    // superview.

    if (height > maxHeight)
        height = maxHeight;

    // now set the height constraint accordingly

    [UIView animateWithDuration:0.25 animations:^{
        self.tableViewHeightConstraint.constant = height;
        [self.view setNeedsUpdateConstraints];
    }];
}

对于后一种基于约束的解决方案,使用autolayout,我们必须小心首先要做的事情是:

For this latter constraint-based solution to work with autolayout, we must take care of a few things first:


  1. 点击按钮组中的中心按钮,确保你的tableview有一个高度限制在这里然后选择添加高度约束:

  1. Make sure your tableview has a height constraint by clicking on the center button in the group of buttons here and then choose to add the height constraint:

然后为该约束添加 IBOutlet

确保调整其他约束,以便在以编程方式调整大小tableview时不会发生冲突。在我的示例中,tableview有一个尾随空间约束,将其锁定在屏幕的底部,因此我必须调整该约束,以便不是被锁定在特定大小,它可能大于或等于一个值,并且具有较低优先级,以便tableview的高度和顶部将统治当天:

Make sure you adjust other constraints so they don't conflict if you adjust the size tableview programmatically. In my example, the tableview had a trailing space constraint that locked it to the bottom of the screen, so I had to adjust that constraint so that rather than being locked at a particular size, it could be greater or equal to a value, and with a lower priority, so that the height and top of the tableview would rule the day:

你在这里做的其他约束将完全取决于你在屏幕下面的屏幕上的其他控件。与往常一样,处理约束有点尴尬,但它确实有效,尽管你的情况中的细节完全取决于你在场景中的其他东西。但希望你能得到这个想法。底线,自动布局,确保调整您的其他约束(如果有)以灵活地考虑更改的tableview高度。

What you do here with other constraints will depend entirely upon what other controls you have on your screen below the tableview. As always, dealing with constraints is a little awkward, but it definitely works, though the specifics in your situation depend entirely upon what else you have on the scene. But hopefully you get the idea. Bottom line, with autolayout, make sure to adjust your other constraints (if any) to be flexible to account for the changing tableview height.

正如您所看到的,如果您不使用自动布局,以编程方式调整tableview的高度要容易得多,但如果您使用autolayout,我会提供两种选择。

As you can see, it's much easier to programmatically adjust the height of a tableview if you're not using autolayout, but in case you are, I present both alternatives.

这篇关于动态更改UITableView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆