I have a UIViewController that implements UITableViewDelegate and UITableViewDataSource. I am customizing the section headers using the viewForHeaderInSection method:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *label = sectionHeaderLabel();
switch (section) {
case 0:
label.text = @"";
break;
case 1:
label.text = @"Points";
break;
case 2:
label.text = @"Problems";
break;
default:
label.text = @"";
break;
}
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
[view addSubview:label];
[label release];
return view;
}
The compiler complains that I have a memory leak related to view and I realize that I should be autoreleasing it. But when I do, my app crashes when I hit the back button to pop the view off the navigation controller.
What’s up with this?
The problem may be related to label instead of view.
Does the sectionHeaderLabel() method return an autoreleased label object? If so you shouldn’t be explicitly releasing label after adding it as a subview to view.
Try removing [label release]; and returning [view autorelease];