Method numberOfRowsInSection: invoked 5 times?

#1

Hello,

I noticed, thanks to the code below, that the required method tableView:numberOfRowsInSection: is invoked 5 times when running the program, whatever the number of randomItems created.
I tried to find out why by creating breakpoint everywhere in my code, but still I couldn’t find out why… Do you have an idea ?

BNRItemsViewController

//
//  BNRItemsViewController.m
//  Homepwner
//
//  Created by Arnaud AMBROSELLI on 12/08/2015.
//  Copyright (c) 2015 Big Nerd Ranch. All rights reserved.
//

#import "BNRItemsViewController.h"
#import "BNRItemStore.h"
#import "BNRItem.h"

@interface BNRItemsViewController ()

@property (nonatomic) BNRItemStore *itemStore;

@end

@implementation BNRItemsViewController

-(BNRItemStore *)itemStore
{
    BNRItemStore *itemStore = [BNRItemStore sharedStore];
    return itemStore;
}

//Designated Initializer is initWithStyle  Changing to init
-(instancetype)init
{
    //ALWAYS call the superclass's designated initializer
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        for (int i = 0; i < 8; i++) {
            BNRItem *item = [self.itemStore createItem];
            NSLog(@"The %@, valued %d has been created. It is at index %lud", item.itemName, item.valueInDollars, (unsigned long)[[[BNRItemStore sharedStore] allItems] indexOfObject:item]);
        }
    }
    return self;
}

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

-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"This tableview %lu rows", (unsigned long)[[self.itemStore allItems] count]);
    return [[self.itemStore allItems] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get a new or recycled cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"
                                                            forIndexPath:indexPath];
    
    //Set the text on the cell with the description of the item that is at the nth index of items, where n = row this cell will appear in on the tableview
    NSArray *items = [self.itemStore allItems];
    BNRItem *item = items[indexPath.row];
    NSLog(@"%@ is in Section %ld Row %ld", item.itemName, (long)[indexPath section], (long)[indexPath row]);

    cell.textLabel.text = [item description];
   
    return cell;
}

-(void)viewDidLoad
{
    [super viewDidLoad];    
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:@"UITableViewCell"];
}

@end

And the log is returning this:

2015-08-13 21:54:47.229 Homepwner[18140:900319] The Fluffy Spork, valued 21 has been created. It is at index 0d
2015-08-13 21:54:47.230 Homepwner[18140:900319] The Fluffy Bear, valued 60 has been created. It is at index 1d
2015-08-13 21:54:47.230 Homepwner[18140:900319] The Shiny Spork, valued 53 has been created. It is at index 2d
2015-08-13 21:54:47.230 Homepwner[18140:900319] The Shiny Spork, valued 27 has been created. It is at index 3d
2015-08-13 21:54:47.230 Homepwner[18140:900319] The Rusty Spork, valued 19 has been created. It is at index 4d
2015-08-13 21:54:47.230 Homepwner[18140:900319] The Rusty Mac, valued 35 has been created. It is at index 5d
2015-08-13 21:54:47.230 Homepwner[18140:900319] The Fluffy Mac, valued 18 has been created. It is at index 6d
2015-08-13 21:54:47.231 Homepwner[18140:900319] The Rusty Bear, valued 22 has been created. It is at index 7d
[color=#FF0000]2015-08-13 21:54:47.249 Homepwner[18140:900319] This tableview has 1 section and 8 rows
2015-08-13 21:54:47.254 Homepwner[18140:900319] This tableview has 1 section and 8 rows
2015-08-13 21:54:47.254 Homepwner[18140:900319] This tableview has 1 section and 8 rows
2015-08-13 21:54:47.297 Homepwner[18140:900319] This tableview has 1 section and 8 rows
2015-08-13 21:54:47.297 Homepwner[18140:900319] This tableview has 1 section and 8 rows[/color]
2015-08-13 21:54:47.298 Homepwner[18140:900319] Fluffy Spork is in Section 0 Row 0
2015-08-13 21:54:47.301 Homepwner[18140:900319] Fluffy Bear is in Section 0 Row 1
2015-08-13 21:54:47.301 Homepwner[18140:900319] Shiny Spork is in Section 0 Row 2
2015-08-13 21:54:47.302 Homepwner[18140:900319] Shiny Spork is in Section 0 Row 3
2015-08-13 21:54:47.302 Homepwner[18140:900319] Rusty Spork is in Section 0 Row 4
2015-08-13 21:54:47.302 Homepwner[18140:900319] Rusty Mac is in Section 0 Row 5
2015-08-13 21:54:47.302 Homepwner[18140:900319] Fluffy Mac is in Section 0 Row 6
2015-08-13 21:54:47.303 Homepwner[18140:900319] Rusty Bear is in Section 0 Row 7