1 | self.tableView.rowHeight = UITableViewAutomaticDimension |
ただし、TableViewからセル選択により画面遷移後、戻ってきたときなど再表示をするとセル位置が調整されていないことがあります。 対策として、一度表示したセルの高さを保持しておいて、estimatedHeightForRowAtIndexPathで値を返します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var cellHeight:Dictionary<string ,= "" float = "" > = [ "" : 0 ] override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { cell.contentView.updateConstraints() self.cellHeight[String(indexPath.item )] = Float(cell.frame.size.height) } override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if (self.cellHeight[String(indexPath.item )] != nil){ return CGFloat(self.cellHeight[String(indexPath.item )]!) } return self.tableView.estimatedRowHeight } </string> |