0

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.

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112

2 Answers2

1

Delegate methods of UITableView can be called at any time during app execution and no one should completely rely on how many times they're called. Lots of external events can force the UITableView to reload its data, so you have to be very careful when you depend on this function call.

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
0

Even though the number of sections is 1 as default, I would try to set it to 1 explicitly:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

Actually it should also work without this, but perhaps there is a bug or something similar forcing the Table View to produce more sections.

Did this help?

fr33g
  • 2,249
  • 1
  • 12
  • 12
  • 1
    Mhhh okay. Try to check the exact stack trace. The only thing I have in mind is something like that: Since you change the header of the section, the size also changes and this triggers at the end a new call of the function. Might be a similarity to this [issue](http://stackoverflow.com/questions/2638359/uitableview-delegate-method-called-twice/4986682#4986682) – fr33g Dec 29 '14 at 13:55