The Hit List Diary #26 – Resizing Table Rows

One question I ran into early on in the development of The Hit List 2, was how to resize a row in a UITableView, and animate it (if you don’t need animation, ‑reloadData is often a very good fit!).

Scanning the docs, -reloadRowsAtIndexPaths:withRowAnimation: sounds a good bet, and it often is. But I ran into a complication:

Several of the rows I needed to resize would contain editable text. The row/cell needs to resize mid-text editing. The trouble with UITableView’s reload methods is they remove the current cell from the view hierarchy, in order to fetch a fresh cell from the data source and display that. The act of removing the view causes text editing to end. Not very friendly!

OK, I’m running low on ideas at this point. Happily, Daniel Tull and Pete Callaway stepped in and pointed out that at the end of a begin/endUpdates pair, table views re-query their delegate for all row heights, and animate the changes accordingly. Excellent!

Just one tiny flaw, “is this actually supported/intentional?” I kept wondering to myself. So I filed a radar (18425998), and happily now the docs have been updated to make it official; the discussion of -beginUpdates describes this feature.

© Mike Abdullah 2007-2015