UiTableViewCellの特定のセルだけ更新する方法

はじめに

やりたかったことは、画面上のUITableViewの特定のセルをクリックされたら、そのセルの中身だけ更新するということでした。
画面イメージは以下になります

※クリック前
f:id:yoppy0066:20150429094908p:plain

※クリック後
f:id:yoppy0066:20150429094916p:plain

今回のサンプルでは高さと背景色を変えてみました

実装方法

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var tableView: UITableView!
    
    var toggle = true

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // 省略(テーブルビューの配置やデリゲートなどなどおきまりのやつ)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // テーブルセルの高さをかえします
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.row == 4 {
            if toggle {
                return 44
            } else {
                return 200
            }
        } else {
            return 44
        }
    }
    
    // テーブル行数をかえします
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    // テーブルセルにデータをセットします
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell", forIndexPath: indexPath) as! UITableViewCell
        if indexPath.row == 4 {
            cell.textLabel?.text = "高さがかわるよ"
            cell.backgroundColor = UIColor.redColor()
        } else {
            cell.textLabel?.text = ""
            cell.backgroundColor = UIColor.clearColor()
        }
        return cell
    }
    
    // ⭐️ここがポイント
    // 5行目がタップされたら更新処理を行います
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == 4 {
            toggle = !toggle
            tableView.reloadRowsAtIndexPaths(tableView.indexPathsForVisibleRows()!, withRowAnimation: UITableViewRowAnimation.Fade)
        }
    }
}

indexPath.row == 4(5行目だったら)とハードコードしてる部分が対象の箇所になります。

以上です