Tuesday, March 1, 2011

Why am I getting two section headers on this grouped table?

I'm sure this is some easy-to-fix issue that I've missed, but I can't figure it out. I've got a grouped table view with 3 sections. I'm using the following code to generate section headers:

-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
NSString *header = @"Header 1";
return header;
}
if (section == 1) {
NSString *header = @"Header 2";
return header;
}
else {
NSString *header = @"Header 3";
return header;
}
}

When my table is displayed, there are two headers for each section - one with white text on a gray bar that stretches across the screen (like the letter separators in the Contacts app), and one in gray text on the table background (which is the one I want). Both headers have identical text. Where is the first header coming from?

From stackoverflow
  • The code you have presented looks completely normal (though it probably is generating a warning for not having a return null; at the end. The issue is somewhere else, but there's no way to determine what that is with what you've given.

    Andy : What else do you need me to post? As far as I know, this is the only place in the code that deals with table headers.
    Ed Marty : Well, the problem you describe is a really bizarre one. Is there any point where you try to change the style of the table view from plain to grouped? Or any where you combine cells from two different tables? Or do you have a method for returning a _view_ for a title instead of a string?
    Andy : Ah, okay. I don't think there's any point where I try to change the type; I drag-and-dropped the tableview onto the view in IB, and set its style to 'grouped.' Nowhere do I combine cells, although it occurs to me that I used the generic cell identifier in both tables; maybe I should change one of them? Not sure how that affects the headers, but maybe it does. And I'm only using strings for the header text; no views.
    Andy : I went into IB and changed the table style from "grouped" to "plain," without changing any of the code in Xcode. Upon running, the grouped table has changed to a plain table, but the headers with the white text on the gray bars are still there, clearly coming from my code above. So, I've managed to code the headers I don't want, but I'm somehow getting those PLUS the headers I DO want - and I STILL don't know how the ones I do want are getting there.

0 comments:

Post a Comment