The HeaderFooter example on Apple.com does exactly what you are saying, but it still shows the titleForHeaderInSection text + what they added to viewForHeaderInSection
No it doesn't. There are two kinds of headers. There's a table header and there are section headers. That example sets the table header to a view and one section header to text. You need to set the section headers to views.
For future reference to anyone else having the same problem.
If you want to change font or font color of the section title you to replace the default view with your own.
bergetun: would you have an example of what the code looks like where you build the new header view?
Thanks.
Quote:
Originally Posted by bergetun
aha. My fault.
Thank you very much.
For future reference to anyone else having the same problem.
If you want to change font or font color of the section title you to replace the default view with your own.
Thanks for the solution. It works beautifully. Couple of questions:
1. Why do we need a customview. Why can't we use just UILabel only?
2. Is there a memory leak here for customview and the label?
Thanks
You could use a UILabel; it's a UIView subclass after all. As a matter of fact, Apple's docs suggest returning a UILabel as an example. If you read salboy's code, though, you'll see that the label bounds X coordinate is set to 10.0 so that it won't bump against the left side of the table. And it's set inside a view because...
I'm not sure specifically how UITableView handles section titles, but since they bump up against each other as they slide out of view I imagine they're simply views being moved directly over the contents of the tableView as it's tracking. In the default case, there's already a view with an embedded label offset inside the view. When you return the section header text via tableView:titleForHeaderInSection:, it's simply setting the default label's text property.
If you'd just passed a UILabel, it would be offset to the bounds of the section header frame, and draw the text at the extreme left of the table. Try it.
Just as a point of style, I probably would have init'ed customView a bit differently:
The view's X coordinate doesn't make much of a difference. It's the label's offset you're worried about.
Oh, and to mimic the standard UITableView behavior, I'd set the header view's backgroundColor to a color with a 0.9 or so alpha component. If you wanted to use gray, it'd look like:
Yes, both the view and label are leaking. Personally, I'd alloc the view as autorelease (because you have to return it) and release the label after you've added it to the view. The view will be released when its superview is released. (Well, actually when its autoreleasePool is drained...)
The error I get in the console is:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SpecialOffersTableViewController 0x3c0c8f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key sectionHeader.'