7

私は実際にインターフェイスビルダーを使用UIScrollViewして内部にを作成します。UITableView私は自分のコンテンツサイズを次のように設定しましたUIScrollView

scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);

ファイルの所有者ビューをUIScrollView

を作成IBOutlet UITableViewし、InterfaceBuilderで設定しました。

ビューがロードされたとき。の正しいコンテンツサイズが表示されますUIScrollView。しかし、私のの正しいコンテンツサイズが表示されませんUITableView。また、のコンテンツサイズを変更すると、両方のコンテンツサイズが関連しているかのようUIScrollViewにコンテンツサイズが変更されます。UITableView

Interface Builderで設定したテーブルビューのコンテンツサイズを維持する方法を知っている人はいますか?

4

4 に答える 4

13

AUITableViewUIScrollViewです。UITableViewの中に埋め込むと便利な場合はほとんどありませんUIScrollView

あなたが本当にこれを望んでいると仮定すると、あなたはどこかで次のことをする必要があるでしょう。おそらく、テーブルビューにデータを入力した後のUIViewControllerinメソッドviewDidAppear:またはどこかで。

// Set the size of your table to be the size of it's contents 
// (since the outer scroll view will handle the scrolling).
CGRect tableFrame = tableView.frame;
tableFrame.size.height = tableView.contentSize.height;
tableFrame.size.width = tableView.contentSize.width; // if you would allow horiz scrolling
tableView.frame = tableFrame;

// Set the content size of your scroll view to be the content size of your 
// table view + whatever else you have in the scroll view.
// For the purposes of this example, I'm assuming the table view is in there alone.
scrollView.contentSize = tableView.contentSize;
于 2012-01-09T14:13:31.497 に答える
1

ScrollViewとともにTableViewの高さを動的に管理する例は、Swiftで機能します。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // Custom Cell
        let cell:TblPortCell = self.tableList.dequeueReusableCellWithIdentifier("cell") as! TblPortCell

        //tableList is the OutLet for the table
        tableList.sizeToFit()
        // Set the size of the table to be the size of it's contents
        // (since the outer scroll view will handle the scrolling).
        let height = tableList.frame.size.height
        let pos = tableList.frame.origin.y
        let sizeOfContent = height + pos + 130
        //scrollView is the Outlet for the Scroll view
        scrollView.contentSize.height = sizeOfContent
        scrollView.sizeToFit()

        cell.selectionStyle = UITableViewCellSelectionStyle.None
        return cell
    }

参照先:

于 2015-09-28T10:59:59.320 に答える
0

それでも問題が解決しない場合は、scrollViewを必要とせずにこれを試してください。

DBDの例をしばらく試してみたところ、frameとcontentSizeを次のように一緒に設定できないようです。

self.tableView.contentSize = CGSizeMake(320, scrollHeight);
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,self.tableView.contentSize.height);

フレームの最大高さを設定しようとしましたが、セルの数が多い場合は、すべてのコンテンツをスクロールする機能を維持しました。このハックはうまく機能しているようで、必要に応じてより多くの条件で変更できます。

int cellCount = [YOURARRAY count];
CGFloat scrollHeight = cellCount*44+44;//for a cell with height of 44 and adding 44 if you have a toolbar at the bottom

self.tableView.contentSize = CGSizeMake(320, scrollHeight);//you can change the width and or let the .xib control the autoresizing

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && cellCount < 14)
{
    //here's were you can add more conditions if you're in landscape or portrait, but this handles 14 cells with a heightForHeaderInSection of 46 in landscape
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight);
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && cellCount < 7)
{
     //same as above but it's for the iPhone in portrait
     self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight);
}

これは確かに機能します。.xib内またはコード内でautoresizingMaskを調整する必要があるかもしれませんが、このハックは、私の場合、すべての異なる変数を処理する唯一の解決策です。

于 2012-08-22T00:31:06.153 に答える
0

以下のメソッドを複数回呼び出します。電話をかけるたびに、より多くのセルがあります。

CGRect tableFrame = tableView.frame;
tableFrame.size.height = tableView.contentSize.height;
tableView.frame = tableFrame;

scrollView.contentSize = tableView.contentSize;
于 2016-12-15T21:01:25.907 に答える