I have a simple project that has a view and this view contains only one table view inside. The table view is grouped and has one simple prototype cell.
I have implemented titleForHeaderInSection
method to customize the section title. When I look at the debug output I saw that the app was printing log statement in the titleForHeaderInSection
multiple times. Sometimes two, sometimes three times. Does anyone know why?
Here is my code:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *initialTitle = @"A title";
NSLog(@"titleForHeaderInSection: %@", initialTitle);
return initialTitle;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *myLabel = [UILabel new];
myLabel.frame = CGRectMake(20, 20, 320, 20);
myLabel.font = [UIFont boldSystemFontOfSize:20];
myLabel.text = @"Test";
UIView *headerView = [UIView new];
[headerView addSubview:myLabel];
return headerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestCell" forIndexPath:indexPath];
return cell;
}
@end
Here is the debug output:
2014-12-29 14:57:30.806 TestTitleForHeaderInSection[5212:118874] titleForHeaderInSection: A title
2014-12-29 14:57:30.812 TestTitleForHeaderInSection[5212:118874] titleForHeaderInSection: A title
2014-12-29 14:57:30.812 TestTitleForHeaderInSection[5212:118874] titleForHeaderInSection: A title
I have also noticed that numberOfRowsInSection
method is called multiple times.