How many times is numberOfSectionsInTableView: invoked?


#1

Here is my code:

[code]//
// ItemViewController.m
// Homepwner
//
// Created by Jay on 10/18/13.
// Copyright © 2013 Jay. All rights reserved.
//

#import “ItemViewController.h”
#import “BNRItemStore.h”
#import “Possession.h”

@implementation ItemViewController

-(id)init
{
self = [super initWithStyle:UITableViewStyleGrouped];

if(self){
    
    itemsLessThan50 = [[NSMutableArray alloc] init];
    itemsMoreThan50 = [[NSMutableArray alloc] init];
    
    for(int i = 0; i< 5; i++){
        [[BNRItemStore sharedStore] createItem];
        
        Possession *temp = [[[BNRItemStore sharedStore] allItems] objectAtIndex:i];
    
        int tempValue = [temp valueInDollars];
        
        if(tempValue <=50)
            [itemsLessThan50 addObject:temp];
        else
            [itemsMoreThan50 addObject:temp];
        
    }
    

    
}

return self;

}

-(id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}

-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{

NSLog(@"2nd!");

int numberOfRows = 0;

for (int i = 0; i < [[[BNRItemStore sharedStore] allItems] count]; i++) {
    switch (section) {
        case 0:
            if ([[[[BNRItemStore sharedStore] allItems] objectAtIndex:i] valueInDollars] <= 50) {
                numberOfRows++;
            }
            
           
            break;
            
        default:
            if ([[[[BNRItemStore sharedStore] allItems] objectAtIndex:i] valueInDollars] > 50) {
                numberOfRows++;
            }
         
            
            break;
    }
}

if(section == 1)
    numberOfRows++;

return numberOfRows;

}

-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

NSLog(@"3rd!");

UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

if(!cell){

cell = [[UITableViewCell alloc]
        initWithStyle:
        UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];

}

if([indexPath section] == 1 && [indexPath row] == 2){
    [[cell textLabel] setText:@"No more items"];
 
    return cell;
}


if([indexPath section]==0){

     Possession *temp = [itemsLessThan50 objectAtIndex:indexPath.row];
    [[cell textLabel] setText:[temp description]];
}
else if([indexPath section] == 1){
    Possession *temp = [itemsMoreThan50 objectAtIndex:indexPath.row];
    [[cell textLabel] setText:[temp description]];

}




    


return cell;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@“1st! = numberofsections”);

return 2;

}

@end
[/code]

log output:

2013-10-18 23:59:03.845 Homepwner[12850:a0b] 1st! = numberofsections 2013-10-18 23:59:03.847 Homepwner[12850:a0b] 1st! = numberofsections 2013-10-18 23:59:03.848 Homepwner[12850:a0b] 2nd! 2013-10-18 23:59:03.848 Homepwner[12850:a0b] 2nd! 2013-10-18 23:59:03.848 Homepwner[12850:a0b] 3rd! 2013-10-18 23:59:03.851 Homepwner[12850:a0b] 3rd! 2013-10-18 23:59:03.851 Homepwner[12850:a0b] 3rd! 2013-10-18 23:59:03.852 Homepwner[12850:a0b] 3rd! 2013-10-18 23:59:03.852 Homepwner[12850:a0b] 3rd! 2013-10-18 23:59:03.853 Homepwner[12850:a0b] 3rd!

Basically I wanted to log when and how many times certain methods are invoked and the outcome has me confused. Why is the numberOfSectionsInTableView: invoked twice in a row before anything else?

Doesn’t it make sense for it to be invoked once, since it sets the number of sections?