しめ鯖日記

swift, iPhoneアプリ開発, ruby on rails等のTipsや入門記事書いてます

UITableViewCellをアニメーション付きでサイズ変更する方法

UITableViewCellのサイズをアニメーション付きで変える実装したのでそのことを書いてみます。

UITableViewCellをアニメーション付きで伸縮する

下のようにheightForRowAtIndexPathで返す値を変更してあげればアニメーション付きで伸縮します。
tableView.beginUpdates()でなくtableView.reloadData()を使うとアニメーションしてくれません。
cellHeightでなく、cellheightを直接変更した場合は変な動きをします。

class MasterViewController: UITableViewController {
    var cellHeight = 44.0

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return CGFloat(cellHeight)
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.beginUpdates()
        cellHeight = (cellHeight == 44.0 ? 100.0 : 44.0)
        tableView.endUpdates()
    }
}