I have a a number of UIViewControllers in my project that implement the UITableViewDataSource and UITableViewDelegate protocols. In Interface Builder I have removed the UIView and replaced it with a subclassed UITableView. In my subclassed UITableView I set a custom backgroundView.
@interface FFTableView : UITableView
@end
@implementation FFTableView
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder]; // Required. setFrame will be called during this method.
self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];;
return self;
}
@end
This all works fine. I have half a dozen or so of these UIViewControllers that all have subclassed UITableViews and they all draw my background image. My background image is dark, so I need to draw my section headers so that they are visible. Googling I find How to change font color of the title in grouped type UITableView? and I implement viewForHeaderInSection in my subclas.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
...
My viewForHeaderInSection is not called. Of course, when I thought about it for a moment, it makes sense. My UIViewControllers are the objects that implement UITableViewDataSource and UITableViewDelegate, and when I put viewForHeaderInSection in one of my UIViewControllers it works just fine.
But, I have half a dozen of these UIViewControllers, all are subclassed to different classes, implementing different functionality. So I decide to put a class inbetween my subclassed UIViewControllers and UIViewController. This way the common code of setting the appearance of the section headers will be in one spot, not in half a dozen spots.
So I write:
@interface FFViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@end
and in here I implement viewForHeaderInSection:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
...
and change my other subclassed controllers to descend from FFViewController, here's one:
@interface FooDetailsViewController : FFViewController
It seems a little odd, having appearance code in 2 places, but it is better than having copies of the same coded scattered all over the place. In FooDetailsViewController I implement some of the Table Protocol methods, but not viewForHeaderInSection. I also get a warning on FFViewController since it doesn't implement all the protocols (which is on purpose, the child class, FooDetailsViewController in this example, fills out the protocol.)
So what's the question?
Not all of the other subclassed UIViewControllers respond to titleForHeaderInSection, so of course when I run I crash on those view controllers. So I try to see if titleForHeaderInSection is implemented:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (![self respondsToSelector:@selector(titleForHeaderInSection)]) {
return nil;
}
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
And respondsToSelector always returns false. So I can kill that section and just force all my subclasses to implement titleForHeaderInSection but that seems wrong.
What's a good way out of this situation? I'm already dislikeing the solution because:
- The code has warnings. (But I can always do How to avoid "incomplete implementation" warning in partial base class to stop them)
- My appearance code is in 2 separate classes
- I need to implement titleForHeaderInSection in code that doesn't need it since it doesn't have sections headers
(Thanks for reading this far!)