Having Trouble With Bronze Challenge

#1

So I’m very much a beginner at both Objective C and iOS, just as a heads up if my code comes across as amateur. I’m attempting the Bronze Challenge but I’m not having great success.

I’ve managed to get two separate sections going and its partially split my sharedStore array into the two, but only partially. Irregardless of whether I instantiate 5 or 8 or 10 objects, only 4 appear in my table when I run the simulator. Would anyone be able to pinpoint the problem here? I don’t understand what would cause the program to either instantiate only 4 objects, OR to only log these 4 objects into my table. Any and all help appreciated, thank you!

#import "BNRItemViewController.h"
#import "BNRItemStore.h"

@implementation BNRItemViewController

/*-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSArray *titles = @[@"Items under $50", @"Items over $50"];
    return titles;
}*/

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

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *header;
    if (section == 0) {
        header = @"Items Under $50";
    }
    
    else if (section == 1) {
        header = @"Items Over $50";
    }
    return header;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    BNRItemStore *store = [BNRItemStore sharedStore];
    NSArray *items = store.allItems;
    NSInteger count = 0;
    
    for (BNRItem *item in items) {
        if (section == 0 && item.valueInDollars > 50) {
            count ++;
        }
        
        else if(section == 1 && item.valueInDollars < 50) {
            count ++;
        }
    }
    return count;
}

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

    //create the cell template
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    
    //access and copy the store of items
    BNRItemStore *store= [BNRItemStore sharedStore];
    NSArray *items = store.allItems;
    
    //create a pointer to an item instance to use for copying into a cell
    BNRItem *item = [items objectAtIndex:indexPath.row];
    
    if (indexPath.section == 0 && item.valueInDollars > 50 ) {
        cell.textLabel.text = [item description];
    }
    
    if (indexPath.section == 1 && item.valueInDollars < 50) {
        cell.textLabel.text = [item description];
    }
    
    return cell;
    
}


-(instancetype) init {
    self = [super initWithStyle:UITableViewStylePlain];
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:@"UITableViewCell"];
    for (int i = 0; i < 8; i++) {
        [[BNRItemStore sharedStore] createItem];
    }
    return self;
}

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


@end
#import "BNRItemStore.h"

@interface BNRItemStore ()

@property (strong, nonatomic) NSMutableArray *storedItems;

@end

@implementation BNRItemStore

-(NSArray *)allItems {
    return [self.storedItems copy];
}

-(BNRItem *)createItem {
    BNRItem *item = [BNRItem randomItem];
    [self.storedItems addObject:item];
    return item;
}

+(instancetype)sharedStore {
    static BNRItemStore *sharedStore;
    if (!sharedStore) {
        sharedStore = [[self alloc] initPrivate];
        sharedStore.storedItems = [[NSMutableArray alloc] initWithArray:[NSArray array]];
    }
    return sharedStore;
}

-(instancetype)initPrivate {
    self = [super init];
    if (self) {
        _storedItems = [[NSMutableArray alloc] init];
    }
    return self;
    
}

-(instancetype)init {
    [NSException raise:@"Singleton"
                format:@"Use + [BNRItemStore sharedStore]"];
    return nil;
}
@end