UITableview titleForHeaderInSection not displaying correctly
I have some VERY simple code to return the title for a section header:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section==0) {
return @"";
} else if (section==1) {
return @"Actions";
} else if (section==2) {
return @"Attached To";
}
return @"";
}
For some reason when the headers are actually displayed on the simulator, half of the time they're simply the first letter and then ellipses. Scrolling up and down (to refresh the header view most likely) will result in the title showing correctly roughly half the time and showing incorrectly the other half of the time.
Do anyone have any idea what could be causing this? I think this is more of a recent development, but it seems to happen in almost all UITableView
s in my application. I don't want to say this is a 3.2 issue, bu开发者_StackOverflow中文版t it might have started happening around then, but either way it must be related to the code somehow. Thank you.
I've figure it out: the actual problem with the code was returning @""
. If you return just a blank string, instead of nil, it will cause a problem with the display of headers and footers.
You need to instead return a nil string to get all headers and footers to display correctly. Returning a space @" "
will still leave the vertical space for the header so that is not a viable option. I've changed all instances of return @"";
to simply return nil;
i copy&pased your code to one of my projects and it works flawless. (sdk 3.2.1)
Maybe the error is in another part? Are creating your own tablecells? If so, are you returning the apropiate height from "tableView:heightForRowAtIndexPath:"? (that problem did hit me once)
When setting the Section Header Titles, I have better success in using an empty NSString that gets set to the corresponding Section, and then later release that string when completed; as well as limiting my use the nested If()Else() statments.
I try to keep it simple and clean. Of course for those tables where I have more than 3 sections, I use a "Switch" statement in place of the If() statements.
The great thing about this function is that it gets called as many times-(number of sections) that you have and will run through the code each time. The NSString *sectionHeader=nil;
gives the compiler a value to be returned, regardless of what is embedded within your If() statements. Otherwise, you get warnings because the compiler doesn't search within the If() statements for your return value.
You can also initialize the String to a "Default" value, e.g. NSString *sectionHeader = @"Default Header Title";
. If non of the If() statements are satisfied, then the allocated default header value will remain the same throughout the function and thus get returned as sectionHeader for the Title.
Basic Structure Below:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//TEMP SECTION STRING HOLDER:
NSString *sectionHeader = nil;
//SET TITLE FOR EACH SECTION:
if(section == 0) {
sectionHeader = @"Section Header No. 1";
}
if(section == 1) {
sectionHeader = @"Section Header No. 2";
}
if(section == 2) {
sectionHeader = @"Section Header No. 3";
}
//RETURN THE SECTION HEADER FOR EACH SECTION:
return sectionHeader;
}
精彩评论