2016-06-28 5 views
0

これは私の問題です。私はUITableViewに識別子 "cellidentifier"を持つCellプロトタイプとしてカスタムUITableViewCellを使用しています。このUITableViewCellでは、空のUIViewをcontentViewのセルに「bubbleView」という名前で追加しました。@IBOutletの接続があり、制約のトップ= 0、ボトム= 0、先行= 0、末尾= 0が追加されています。コントローラでiOS + Swift 2:空のビューでUITableViewCellの動的な高さが機能しない

(UITableViewDataSourceとUITableViewDelegateから継承する)、のviewDidLoad関数に私はこの行を追加しました:そして

override func viewDidLoad() { 
    ... 
    self.mytable.delegate = self 
    self.mytable.dataSource = self 
    self.mytable.estimatedRowHeight = 100.0 
    self.mytable.rowHeight = UITableViewAutomaticDimension; 
    ... 
} 

は、私は、このようにUITableViewDelegateの方法を実装:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("cellidentifier") as! myCustomCell! 
    if (cell == nil) { 
     cell = myCustomCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cellidentifier") 
    } 
    let myNewView = UIView(frame: CGRectMake(0, 10, randomWidth, random Height)) 
    cell.bubbleView.addSubview(myNewView) 
    return cell 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return items.count 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

そのコードで、私はセルにmyNewViewを追加しますが、セルはmyNewViewの高さに応じて実際の高さに調整されません。たとえば、myNewViewの高さが134の場合、カスタムビューのセルの高さは常に44となります。チュートリアルで言うようにlayoutIfNeededプロパティを追加しましたが、うまくいきません。誰でもこれの解決策がありますか?

答えて

0

空のビューに高さの制約がありますか?バブルビューをスーパービューのエッジに固定しているだけの場合は、オートレイアウトがセルの高さを把握するのに十分ではないと思います。

また、プロトタイプセルの場合は、サイズインスペクタに移動します。テーブルビューのセルはどのように設定されていますか? 「デフォルト」vs「カスタム」として設定されていると、問題が解決する場合もありません。

セルを本当に自動サイズ調整したい場合は、実際に高さを計算できるように十分な制約を設定する必要があります。

0

解決策:これは制約に関するトピックです。 Interface Builderで気づいた場合は、制約が非常に明確である必要があります。したがって、空のビューの上、下、前と後ろの割り当てがあいまいです。その後、溶液(トップ、幅と高さに私の場合には)あなたのニーズに応じてmyNewViewと参照してコンテナ(cell.bubbleView)を表示するために、高さの制約を設定myNewViewする制約を追加です:

//Create my view 
let myNewView = UIView(frame: CGRectMake(0, 10, randomWidth, random Height)) 

myNewView.translatesAutoresizingMaskIntoConstraints = true 

//Assign top constraint according with container (cell.bubbleView) 
let pinTop = NSLayoutConstraint(item: myNewView, attribute: .Top, relatedBy: .Equal, toItem: cell.bubbleView, attribute: .Top, multiplier: 1.0, constant: 10) 
cell.bubbleView.addConstraint(pinTop) 

//set width constraint of myNewView 
let pinWidth = NSLayoutConstraint(item: myNewView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: myNewView.frame.size.width) 
cell.bubbleView.addConstraint(pinWidth) 

//set height constraint of myNewView 
let pinHeight = NSLayoutConstraint(item: myNewView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: myNewView.frame.size.height) 
cell.bubbleView.addConstraint(pinHeight) 

//set height constraint of container according the myNewView plus top and bottom space 
let pinHeightView = NSLayoutConstraint(item: cell.bubbleView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: myNewView.frame.size.height+20) 
cell.bubbleView.addConstraint(pinHeightView) 

cell.bubbleView.addSubview(myNewView) 

、最終的にドン高さのセルを動的に作るようにconfigure UITableViewを忘れないでください。

self.mytable.estimatedRowHeight = 100.0 
self.mytable.rowHeight = UITableViewAutomaticDimension; 

そして、それは完璧に動作します

関連する問題