2017-01-20 17 views
0

の一部を非表示解除するとき、私はUITableViewAutomaticDimensionに設定rowHeightの財産でありますテーブルビューでのViewControllerを持って展開していません。それはカスタムセルを持っています。セルにはスタックビューが含まれています。そのスタックビューの上部にはボタンがあり、下部にはテキストフィールドがあります。テキストフィールドは最初は非表示に設定されています。セルはstackview

UITableView with prototype cell containing stackview

ボタンをタップすると、テキストフィールドを再表示します。セルしかし、展開されません。期待

Screenshot of iOS Simulator

は、セルが全体stackviewを表示するために展開することです。なぜこれは機能しませんか?

(Iは、細胞を再ロードすることによって動作するようにそれを得ているが、これはViewControllerをのコードを必要と醜い回避策です。)のGithub上

プロジェクト:残念ながら https://github.com/bvankuik/TestResizingCell

答えて

1

私は次のように問題を「修正」しました。ビューコントローラでは、行にテキストフィールドが隠されているか可視であるかどうかを確認します。私はボタンを削除して、ただdidSelectRowAtを使用します。完全なコードについて

struct NameCellData { 
    var name: String 
    var isTextFieldHidden: Bool 
} 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    private var items: [NameCellData] = [] 

    @IBOutlet weak var tableView: UITableView! 

    // MARK: - UITableViewDataSource, UITableViewDelegate 

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell 
     cell.nameLabel.text = self.items[indexPath.row].name 
     cell.nameCorrectionTextField.isHidden = self.items[indexPath.row].isTextFieldHidden 
     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     self.items[indexPath.row].isTextFieldHidden = !self.items[indexPath.row].isTextFieldHidden 
     tableView.reloadRows(at: [indexPath], with: .automatic) 
    } 

    // MARK: - View cycle 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let names = [ 
      "Allen","Upton","Hu","Yuli","Tiger","Flynn","Lev","Kyle","Sylvester","Mohammad", 
      "Harlan","Julian","Sebastian","Porter","Preston","Palmer","Jakeem","Micah","Stephen","Tucker" 
     ] 
     for name in names { 
      let nameCellData = NameCellData(name: name, isTextFieldHidden: true) 
      items.append(nameCellData) 
     } 

     self.tableView.estimatedRowHeight = 150 
     self.tableView.rowHeight = UITableViewAutomaticDimension 
    } 

} 

、GitHubの上でオリジナルのテストプロジェクトのブランチを参照してください:それは、細胞を再ロード https://github.com/bvankuik/TestResizingCell/tree/withReload

0

、自己サイズのセルの高さそれらの制約を変更すると自動的に変更されません。 あなたが生成中のtableViewは、デリゲートのメソッドがコールされた場合:numberOfSectionsnumberOfRowsInSectioncellForRowAt、等...

それは同様tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)の場合です。つまり、セル内の制約を変更した後にtableViewをリロードしないと、これらのデリゲートのメソッドは呼び出されず、行レイアウトの高さは自動レイアウトによって再度計算されません。

テーブルビューをリロードするのではなく、テキストフィールドを表示した直後にcell.layoutIfNeeded()を呼び出すことをお勧めします。

+0

はcell.layoutIfNeeded(IS)ソリューションとして意味しましたか?私はその行をどこに置くことをお勧めしますか? –

関連する問題