After a few months of .NET reporting/SSIS development work, I’m back to an iPhone project this week. One enhancement I added yesterday was a better formatted table section title in a UITableView. Before, the section title is either a bunch of unformatted (also incorrectly by locale) dates (e.g. 2009-09-30), or times (e.g. 14:58) straight from the data source. The enhancement/bug fix is to format the date or time to be locale aware so the title would either be “Wed Sep, 30 2009” or “2:58 PM” if you are in the US.
Pretty straightforward I thought, and after a couple of trips to
NSDateFormatter and use the output in UITableView’s
titleForHeaderInSection:section method, it was all working very well in the simulator. That was until I put the app onto my iPhone for some real in-device testing.
The scrolling performance in the table was horrible! My first thought was that it had to do with the background view I added to the custom table cell view for colouring the table cell background. But after nearly an hour of debugging through the code I still couldn’t find anything wrong.
Turns out that the
titleForHeaderInSection:section method is not just called once per controller instantiation. It is called once per table cell display!
Once I moved the code to format the section title into
viewDidLoad and cached a copy of the nicely formatted titles in an array, the scrolling is back to normal speed.