UITableView data source/delegate flow of control
UITableView data source/delegate flow of control
- Subject: UITableView data source/delegate flow of control
- From: WT <email@hidden>
- Date: Wed, 27 May 2009 17:38:29 +0200
Hello...
So, in order to better understand the flow of control when accessing a
UITableView instance, I created a tiny project with a single section,
having a single row, and then implemented every one of the 29 data
source and delegate methods to do trivial things in addition to
printing a log message to the console window. The result surprised me
in that two methods that I would think would be called only once per
section each are each being called twice per section. Take a look at
the log messages below (for the boring case of starting the app and
then immediately closing it, without any other interaction) and note
that
-tableView: heightForHeaderInSection: and -tableView:
heightForFooterInSection:
are both being called twice in sequence for the single section. Note
also that the same does not happen to the method
-tableView: heightForRowAtIndexPath:
which is called only once per row per section, as expected.
Now, of course, it's probably the case that there is some private code
being executed between those two calls to the same method, but I would
have expected the result of the first call to be cached, obviating the
second call.
It's also interesting that
-tableView: viewForHeaderInSection: and -tableView:
titleForHeaderInSection:
(and the corresponding methods for the footer) are also being called
twice in the process of drawing the row cell.
[Session started at 2009-05-27 17:20:38 +0200.]
2009-05-27 16:46:41.065 TableViewExample[45503:20b]
2009-05-27 16:46:41.067 TableViewExample[45503:20b] -
numberOfSectionsInTableView:
2009-05-27 16:46:41.068 TableViewExample[45503:20b]
2009-05-27 16:46:41.068 TableViewExample[45503:20b] -tableView:
viewForHeaderInSection:
2009-05-27 16:46:41.069 TableViewExample[45503:20b] -tableView:
titleForHeaderInSection:
2009-05-27 16:46:41.069 TableViewExample[45503:20b] -tableView:
heightForHeaderInSection:
2009-05-27 16:46:41.069 TableViewExample[45503:20b] -tableView:
heightForHeaderInSection:
2009-05-27 16:46:41.070 TableViewExample[45503:20b]
2009-05-27 16:46:41.070 TableViewExample[45503:20b] -tableView:
viewForFooterInSection:
2009-05-27 16:46:41.071 TableViewExample[45503:20b] -tableView:
titleForFooterInSection:
2009-05-27 16:46:41.071 TableViewExample[45503:20b] -tableView:
heightForFooterInSection:
2009-05-27 16:46:41.072 TableViewExample[45503:20b] -tableView:
heightForFooterInSection:
2009-05-27 16:46:41.072 TableViewExample[45503:20b]
2009-05-27 16:46:41.073 TableViewExample[45503:20b] -tableView:
numberOfRowsInSection:
2009-05-27 16:46:41.073 TableViewExample[45503:20b] -tableView:
heightForRowAtIndexPath:
2009-05-27 16:46:41.074 TableViewExample[45503:20b]
2009-05-27 16:46:41.074 TableViewExample[45503:20b] -
sectionIndexTitlesForTableView:
2009-05-27 16:46:41.082 TableViewExample[45503:20b]
2009-05-27 16:46:41.083 TableViewExample[45503:20b] -tableView:
cellForRowAtIndexPath:
2009-05-27 16:46:41.084 TableViewExample[45503:20b] -tableView:
indentationLevelForRowAtIndexPath:
2009-05-27 16:46:41.084 TableViewExample[45503:20b] -tableView:
canEditRowAtIndexPath:
2009-05-27 16:46:41.085 TableViewExample[45503:20b] -tableView:
willDisplayCell: forRowAtIndexPath:
2009-05-27 16:46:41.085 TableViewExample[45503:20b]
2009-05-27 16:46:41.086 TableViewExample[45503:20b] -tableView:
viewForHeaderInSection:
2009-05-27 16:46:41.086 TableViewExample[45503:20b] -tableView:
titleForHeaderInSection:
2009-05-27 16:46:41.086 TableViewExample[45503:20b] -tableView:
heightForHeaderInSection:
2009-05-27 16:46:41.087 TableViewExample[45503:20b]
2009-05-27 16:46:41.087 TableViewExample[45503:20b] -tableView:
viewForFooterInSection:
2009-05-27 16:46:41.087 TableViewExample[45503:20b] -tableView:
titleForFooterInSection:
2009-05-27 16:46:41.088 TableViewExample[45503:20b] -tableView:
heightForFooterInSection:
2009-05-27 16:46:41.089 TableViewExample[45503:20b]
2009-05-27 16:46:41.090 TableViewExample[45503:20b] -tableView:
viewForHeaderInSection:
2009-05-27 16:46:41.090 TableViewExample[45503:20b] -tableView:
titleForHeaderInSection:
2009-05-27 16:46:41.094 TableViewExample[45503:20b]
2009-05-27 16:46:41.094 TableViewExample[45503:20b] -tableView:
viewForFooterInSection:
2009-05-27 16:46:41.095 TableViewExample[45503:20b] -tableView:
titleForFooterInSection:
Terminating in response to SpringBoard's termination.
The results above are for the case when
-tableView: viewForHeaderInSection: and -tableView:
viewForFooterInSection:
both return nil. It turns out that if the view that's returned isn't
nil, the title and height calls are still made exactly as in the nil
case, but the view frame is completely ignored. Instead, the view is
laid out left-adjusted, with the full width of the screen, but with
the height returned by the call to the height method. See the log
messages below for the case when both header and footer views are non-
nil.
[Session started at 2009-05-27 17:25:51 +0200.]
2009-05-27 17:31:53.348 TableViewExample[46001:20b]
2009-05-27 17:31:53.351 TableViewExample[46001:20b] -
numberOfSectionsInTableView:
2009-05-27 17:31:53.354 TableViewExample[46001:20b]
2009-05-27 17:31:53.355 TableViewExample[46001:20b] -tableView:
viewForHeaderInSection:
2009-05-27 17:31:53.357 TableViewExample[46001:20b] -tableView:
titleForHeaderInSection:
2009-05-27 17:31:53.359 TableViewExample[46001:20b] -tableView:
heightForHeaderInSection:
2009-05-27 17:31:53.360 TableViewExample[46001:20b] -tableView:
heightForHeaderInSection:
2009-05-27 17:31:53.361 TableViewExample[46001:20b]
2009-05-27 17:31:53.362 TableViewExample[46001:20b] -tableView:
viewForFooterInSection:
2009-05-27 17:31:53.363 TableViewExample[46001:20b] -tableView:
titleForFooterInSection:
2009-05-27 17:31:53.364 TableViewExample[46001:20b] -tableView:
heightForFooterInSection:
2009-05-27 17:31:53.366 TableViewExample[46001:20b] -tableView:
heightForFooterInSection:
2009-05-27 17:31:53.367 TableViewExample[46001:20b]
2009-05-27 17:31:53.368 TableViewExample[46001:20b] -tableView:
numberOfRowsInSection:
2009-05-27 17:31:53.369 TableViewExample[46001:20b] -tableView:
heightForRowAtIndexPath:
2009-05-27 17:31:53.381 TableViewExample[46001:20b]
2009-05-27 17:31:53.413 TableViewExample[46001:20b] -
sectionIndexTitlesForTableView:
2009-05-27 17:31:53.421 TableViewExample[46001:20b]
2009-05-27 17:31:53.422 TableViewExample[46001:20b] -tableView:
cellForRowAtIndexPath:
2009-05-27 17:31:53.425 TableViewExample[46001:20b] -tableView:
indentationLevelForRowAtIndexPath:
2009-05-27 17:31:53.426 TableViewExample[46001:20b] -tableView:
canEditRowAtIndexPath:
2009-05-27 17:31:53.427 TableViewExample[46001:20b] -tableView:
willDisplayCell: forRowAtIndexPath:
2009-05-27 17:31:53.427 TableViewExample[46001:20b]
2009-05-27 17:31:53.427 TableViewExample[46001:20b] -tableView:
viewForHeaderInSection:
2009-05-27 17:31:53.428 TableViewExample[46001:20b] -tableView:
titleForHeaderInSection:
2009-05-27 17:31:53.428 TableViewExample[46001:20b] -tableView:
heightForHeaderInSection:
2009-05-27 17:31:53.429 TableViewExample[46001:20b]
2009-05-27 17:31:53.429 TableViewExample[46001:20b] -tableView:
viewForFooterInSection:
2009-05-27 17:31:53.430 TableViewExample[46001:20b] -tableView:
titleForFooterInSection:
2009-05-27 17:31:53.430 TableViewExample[46001:20b] -tableView:
heightForFooterInSection:
2009-05-27 17:31:53.430 TableViewExample[46001:20b]
2009-05-27 17:31:53.431 TableViewExample[46001:20b] -tableView:
viewForHeaderInSection:
2009-05-27 17:31:53.431 TableViewExample[46001:20b]
2009-05-27 17:31:53.432 TableViewExample[46001:20b] -tableView:
viewForFooterInSection:
Terminating in response to SpringBoard's termination.
Anyhow, I thought the flow of control might be a useful thing for
other iPhone noobs like me to know, so I'm posting this.
Wagner
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden