views:

1460

answers:

2

I have a custom UIView subclass that i'm trying to use as a header for one of my grouped tableview sections. I save an instance of that view in the tableViewController and use that to return the height for the header section as well as the view itself. the problem is that somehow that instance variable changes from a UIView to a CALayer in the middle of a reloadData call which causes a crash, since the instance has a special method to return it's expected height. this is the code that crashes:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{   
    if (section == 0)
    {
        return [self.dataHeader frameHeight];
    }
    return 0.0f; 
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {    
        return self.dataHeader;
    }
    return nil; 
}

I set a breakpoint at the first return in the if block of the heightForHeaderInSection method, and it hits it 4 times; the first three return the dataHeader successfully, while the fourth time shows it to be a CALayer and crashes with a doesNotRecognizeSelector exception (my tableview has 2 sections if that makes a difference). Is there any reason why this happens, and is there a way to stop it?

A: 

The problem seems to be that you have 2 sections and somehow the app thinks there are 4. Here's how I would debug this problem:

1) What is numberOfSectionsInTableView returning (is it implemented)?

I assume that each header method should be called n times, where n is the number of sections in your table. I would also assume that the app asks the aforementioned delegate what n is.

2) What are the values of section each time these delegates are called?

There should only be one call per section, unless I'm missing something, and I would be amazed if the delegate gets called more than once with the same section value.

TahoeWolverine
+2  A: 

What does your initialization code for dataHeader look like? When you initialize dataHeader, are you properly retaining it?

My guess is that your dataHeader view is getting released before you intended.

Jonathan Arbogast
You're right; that was a problem. I actually had left an autorelease there from a refactor. I'll try it tomorrow when i get the chance to see if that fixes it :).
Kevlar