2016-06-27 14 views
1

私は、テーブルビューを含むスクロールビューを持っているストーリーボードを構築しています。 最初に私がしたのは、テーブルビューでスクロールを無効にすることでした。さもなければ、2つのネストされたスクロールバー、erkを取得します! しかし、私の問題は、テーブルビューの高さがセルの高さの合計に等しくなるようにすることです。 (セルの高さはコメントを表示するため、さまざまです)。UITableViewはスクロール可能ではありませんが、十分な高さです。

PS:まもなく

self.commentsTableView.estimatedRowHeight = 90.0 
self.commentsTableView.rowHeight = UITableViewAutomaticDimension 

:ダイナミックセルの高さのために、私はこれを使用し、私は身長変数リストのように動作するためにテーブルビューを望みます。

答えて

3

あり、これを行うには、他の方法かもしれないが、私の場合は

だから、あなたがチェックできるように、最初のtableViewのすべてのセルの負荷を聞かせている何をすべきか、すべてのtableView細胞で、これを行ってきました、以下のように

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 

    //Update tableView Offset 
    if indexPath.row() == (tableView.indexPathsForVisibleRows.last! as! NSIndexPath).row { 
     //End of loading all Visible cells 
     self.updateTableViewOffsetForTableView(self.tableView, withHeightConstraint: self.heightConstraintTableView) 
     //If cell's are more than 10 or so that it could not fit on the tableView's visible area then you have to go for other way to check for last cell loaded 
    } 
} 

をロードされている。そして、この後、あなたのtableViewのために必要な高さを取得してのtableViewをスクロールせずに見える全ての細胞へのtableViewの高さを増加させるためのtableViewの高さ制約にこの高さを設定する必要がありますが、あなたは必要になりますscrollViewに移動します。

それだ
func updateTableViewOffsetForTableView(tableView: UITableView, withHeightConstraint heightConstraintToBeUpdated: NSLayoutConstraint) { 

    var heightRequiredForTable: CGFloat = tableView.contentSize.height 
    //Set some random height first, it is easy for tableView to shrink its height to high to low. 
    heightConstraintToBeUpdated.constant = 300 
    self.view!.layoutIfNeeded() 
    self.view!.setNeedsUpdateConstraints() 

    //Update the height constraint with height required for tableView 
    heightRequiredForTable = tableView.contentSize.height 
    heightConstraintToBeUpdated.constant = heightRequiredForTable 
    self.view!.layoutIfNeeded() 
    self.view!.setNeedsUpdateConstraints() 
} 

あなたが正しく設定され、あなたのscrollViewの制約を設定している場合は、あなたが期待しているとして、それは間違いなく動作します...

更新

私は、ダイナミックセルを有するデモを作成しました

:、以下の出力、

スクロール可能なテーブルビューを参照してください

スクロールScrollView:

enter image description here

+0

おかげで、しかしcontentSize.heightはestimatedRowHeight *行の数を返しますが、私の行の高さが可変であるので、私は動作しません。アイデア? – NathanVss

+0

いいえ、contentSizeはestimatedRowHeightを返しません。tableViewがすべてのセルを表示するために必要な高さを返します。私は動的な高さのセルを持つデモを作成し、期待どおりに動作しています。更新された回答を参照してください。 –

関連する問題