UITableView の高さを動的に変更する 質問する

UITableView の高さを動的に変更する 質問する

テーブルビューの高さを、セルの高さの合計に基づいて別のビューコントローラから動的に変更したいのですが、それは可能ですか? ありがとうございます

アドオン:

基本的に私が持っているのは、画面の半分にコンテナビューを持つ UserProfileViewController です。そこに、他のさまざまなビューコントローラを追加します。

ここに画像の説明を入力してください

ここに画像の説明を入力してください

壁ボタンの場合、ビューコントローラとそれに続くテーブルビューを追加する方法は次のとおりです。

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

そして、そのビューコントローラでテーブルビューを初期化します。

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

セルの高さの合計を計算していますが、問題は、セルの高さメソッドがテーブルビューのゲッターが呼び出された後に呼び出されることにあります。そのため、セルの高さメソッドがすべてのセルに対していつ完了するかを知る何らかの方法が必要になり、その後テーブルビューのサイズを変更できます。ありがとうございます

ベストアンサー1

テーブルビューの内容に基づいてテーブルの高さを変更するシステム機能はありません。とはいえ、内容に基づいて、具体的にはテーブルビューの に基づいて、プログラムでテーブルビューの高さを変更することは可能ですcontentSize(手動で高さを計算するよりも簡単です)。iOS 6 の一部である新しい自動レイアウトを使用しているかどうかによって、詳細のいくつかが異なります。

ただし、テーブル ビューの基盤となるモデルを で設定していると仮定するとviewDidLoad、テーブル ビューの高さを調整する場合は、 で次のように実行できますviewDidAppear

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

    [self adjustHeightOfTableview];
}

同様に、テーブルビューに対して を実行する場合(または行を追加または削除する場合) は、次のようにreloadData手動でも呼び出すようにしてください。adjustHeightOfTableView

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

    [self adjustHeightOfTableview];
}

では、どうすればいいのかという疑問が生じますadjustHeightOfTableview。残念ながら、これは iOS 6 の自動レイアウトを使用するかどうかに関係します。自動レイアウトがオンになっているかどうかを確認するには、ストーリーボードまたは NIB を開いて「ファイル インスペクター」に移動します (たとえば、option+ command+を押す1か、右側のパネルの最初のタブをクリックします)。

ここに画像の説明を入力してください

自動レイアウトがオフになっていると仮定してみましょう。その場合、テーブルビューの をadjustHeightOfTableview調整するだけで済みます。frame

- (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テーブルビューの高さの制約が調整されます。

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

この後者の制約ベースのソリューションを自動レイアウトで動作させるには、まずいくつかの点に注意する必要があります。

  1. ここにあるボタンのグループの中央のボタンをクリックして、テーブルビューに高さの制約があることを確認し、高さの制約を追加することを選択します。

    高さ制限を追加

  2. 次に、IBOutletその制約に を追加します。

    IBOutletを追加

  3. プログラムでテーブルビューのサイズを調整する場合は、他の制約が競合しないように調整してください。私の例では、テーブルビューには画面の下部に固定する末尾のスペース制約があったため、その制約を調整して、特定のサイズに固定するのではなく、値より大きいか等しい値で、優先順位を低くして、テーブルビューの高さと上端が優先されるようにする必要がありました。

    その他の制約を調整する

    ここで他の制約を使って何をするかは、テーブルビューの下の画面にどのようなコントロールがあるかによって決まります。いつものように、制約の扱いは少し面倒ですが、確実に機能します。ただし、状況の詳細は、シーンに他に何があるかによって決まります。しかし、うまくいけば、アイデアは伝わるでしょう。要するに、自動レイアウトでは、テーブルビューの高さの変化に対応できるように、他の制約 (ある場合) を柔軟に調整するようにしてください。

ご覧のとおり、自動レイアウトを使用していない場合は、プログラムでテーブルビューの高さを調整する方がはるかに簡単ですが、自動レイアウトを使用している場合のために、両方の代替案を紹介します。

おすすめ記事