俺、サービス売って家買うんだ

Swift, Kotlin, Vue.js, 統計, GCP / このペースで作ってればいつか2-3億で売れるのがポっと出来るんじゃなかろうか

UITableViewにinsertRowsAtIndexPathsを実行した後にheaderとfooterが消える問題

これでもかというくらいハマったのでメモ。

現象

やろうとしたこと
UITableViewにinsertRowsAtIndexPathsメソッドを発行してセルの数を増やそうとしいた

問題
セルが追加されたあとにセクションのheaderとfooterが消える!
このコード実行後↓

myTableView.beginUpdates()
myTableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None)
myTableView.endUpdates()

理由

原因はviewForHeaderInSectionでUITableViewCellを継承したオブジェクトを返していたことでした。 本来TableViewCellのheader/footerはUITableViewCellを継承していないUIViewでなくてはならないようです。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        // HeaderCellがUITableViewCellを継承している
        let cell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! HeaderCell        
        return cell
}

適当に直して、これならheader/footerはなくなりません。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()      
        return view
}

苦しい戦いであった。
(。・ˇ_ˇ・。)オラッオラッ