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

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

问题描述

我想根据其单元格高度的总和从另一个视图控制器更改我的 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:

在墙上按钮的情况下,这是我添加视图控制器及其后续表格视图的方式:

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;
}

在那个视图控制器中,这是我初始化表格视图的地方:

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之后被调用.所以我需要某种方法来知道何时为所有单元格完成单元格的高度方法,然后我可以调整我的表格视图的大小.谢谢

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 的内容更改 table 高度的系统功能.话虽如此,可以根据内容以编程方式更改 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 中配置你的 table view 的底层模型,如果你想调整 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];
}

同样,如果您曾经为 tableview 执行 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 并转到文件检查器"来确定是否打开了自动布局(例如,按 option+command+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 的 frame:

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];
    }];
}

对于后一种基于约束的解决方案与自动布局一起使用,我们必须首先注意以下几点:

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:

Then add an IBOutlet for that constraint:

确保您调整其他约束,以便在您以编程方式调整大小 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 下方的屏幕上的其他控件.与往常一样,处理约束有点尴尬,但它绝对有效,尽管您所处的具体情况完全取决于您在现场的其他情况.但希望你能明白.最重要的是,使用自动布局,请确保调整其他约束(如果有)以灵活应对不断变化的 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 的高度要容易得多,但如果您使用了自动布局,我会提供两种选择.

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天全站免登陆