0

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:

  1. The code has warnings. (But I can always do How to avoid "incomplete implementation" warning in partial base class to stop them)
  2. My appearance code is in 2 separate classes
  3. I need to implement titleForHeaderInSection in code that doesn't need it since it doesn't have sections headers

(Thanks for reading this far!)

Community
  • 1
  • 1
Paul Cezanne
  • 8,629
  • 7
  • 59
  • 90
  • ell i m really sorry if u mis understood u. can u tell me whether all the controllers that are having UITableView Delegate have same kind of cells i mean like in size and what text is on those cells... .. – WaaleedKhan Apr 18 '12 at 15:46
  • @Addicted: I think you have a question of your own you want to ask. Just create a new question. – Paul Cezanne Apr 18 '12 at 15:59
  • SHOULD I KNOW WHATS THAT MEAN – WaaleedKhan Apr 18 '12 at 16:27
  • I'm sorry. I did not understand your first comment. I don't know why you are asking me those questions. They don't seem relevant. I took a guess that you have a technical problem of your own and you were hoping that my answers to your comment would help you. I don't see how that is possible. So, if you do have a question, just ask it. Click the "Ask Question" link in the upper right hand corner of the page. – Paul Cezanne Apr 18 '12 at 17:15

1 Answers1

2

In your respondsToSelector code you have given the wrong name for the selector you are testing for. It should be:

if (![self respondsToSelector:@selector(tableView:titleForHeaderInSection:)]) {
    return nil;
}

If you make that change your test should work, and your subclasses can then optionally implement tableView:titleForHeaderInSection: or not.

jonkroll
  • 15,682
  • 4
  • 50
  • 43