2017-07-04 18 views
0

テーブルビューの高さを調整して、一度にxセルだけを表示したい。セルのレイアウトは設定可能なので、事前にその高さを知ることはできません。x表示セルのセル高さに基づいてテーブルビューの高さを設定する

簡単にするために、固定テキストを縦に積み重ねた2つのラベルを持つセルの最も簡単な例を設定しようとしました。

MyCustomViewCell.swift Iは、テーブルビューを初期化した後に基づくcellForRowAtIndexPath上の実際の高さを設定する場合(下1000優先度)の高さの制約を設定することにより、所望の動作を達成するために管理している

class MyCustomViewCell: UITableViewCell { 

    let label: UILabel 
    let label2: UILabel 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     label = UILabel() 
     label2 = UILabel() 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 

     self.contentView.addSubview(label) 
     self.contentView.addSubview(label2) 

     label.translatesAutoresizingMaskIntoConstraints = false 

     label.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true 
     label.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true 
     label.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true 

     label2.translatesAutoresizingMaskIntoConstraints = false 
     label2.topAnchor.constraint(equalTo: label.bottomAnchor).isActive = true 
     label2.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true 
     label2.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true 
     label2.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true 

     label.text = "string 1" 
     label2.text = "string 2" 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

セルのフレームと、私が見たいと思ったセルの量(この場合は5)。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    let cellIdentifier = "cellIdentifier" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let tableView = UITableView(frame: .zero, style: .plain) 
     setupTableViewConstraints(tableView: tableView) 

     tableView.register(MyCustomViewCell.self, forCellReuseIdentifier: cellIdentifier) 
     tableView.dataSource = self 
     tableView.delegate = self 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    func setupTableViewConstraints(tableView: UITableView) { 
     self.view.addSubview(tableView) 
     tableView.translatesAutoresizingMaskIntoConstraints = false 

     tableView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50).isActive = true 
     let heightConstraint = tableView.heightAnchor.constraint(equalToConstant: 300) 
     heightConstraint.priority = 900 
     heightConstraint.isActive = true 

     tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10).isActive = true 
     tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -10).isActive = true 

     tableView.layer.borderColor = UIColor.black.cgColor 
     tableView.layer.borderWidth = 0.5 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 10000 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath as IndexPath) as! MyCustomViewCell 

     tableView.heightAnchor.constraint(equalToConstant: cell.frame.height*5).isActive = true 
     return cell 
    } 
} 

しかし、このアプローチは私に権利いないようです。 cellForRowAtIndexPathが実行されるたびに、「ダミー」高さを設定してから後で調整してテーブルビューの高さに設定することから始まります。

より洗練されたソリューションの方向性を指摘できますか?

+0

あなたは「tableView.tableFooterView =のUIView(フレームを試してみました: CGRect.zero) 'となります。あなたのテーブルビューは効果的に細胞のサイズのように見えます。 –

答えて

0

あなたの例では、セルの読み込みを要求するたびにテーブルビューの高さを更新しています。

これはうまくいくかもしれませんが、セルが表示される前であっても、このメソッドが常にtableviewによって呼び出されることを知っているようです。

テーブルビューの読み込みが完了してから高さが更新されるまで待つのはなぜですか?

あなたは、細胞がそのような完了の閉鎖で、おそらく表示されるか、されようとしているときにのみtriggedされている代わりにwillDisplayCellでこれを行うことができます:

self.tableView.reloadData() 
DispatchQueue.main.async { 
    // This closure is only called when the data has been reloaded 
    // and thus will enable you to calculate the final content height 
} 
関連する問題