Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:13:19+00:00 2026-06-02T06:13:19+00:00

I have a a number of UIViewControllers in my project that implement the UITableViewDataSource

  • 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!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:13:20+00:00Added an answer on June 2, 2026 at 6:13 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of views that use UITableView. (I use UIViewController and implement
I'm working on a project were we have number (5 at the moment) of
I have a number of code value tables that contain a code and a
I have a number of views that include tableviews, but other controls as well.
That is what I suppose to have. UIViewController with a dynamic number of draggable
(first UIViewController)I have several UITextfields that will have some number value within them. I
I have following problem: I have a NSDictionary here: @interface ENSListViewController : UIViewController <UITableViewDataSource,
I have a UIViewController in which I have a UITextView added from interface builder.
I have one project using storyboard. I have one UIView and I put some
I'm stuck with the following scenario and appreciate any help/advice.. Requirement I have number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.