My solution for silver challenger

#1

according to “MVC”, We should finish setup data in “BNRItemStore”.
In “BNRItemStore.h”, i add a new method for creating arrays which contained there data “all items”,“itemsUnder50”,“itemsAbove50”.

BNRItemStore.h

[code]#import <Foundation/Foundation.h>
@class BNRItem;

@interface BNRItemStore : NSObject

@property(nonatomic,readonly)NSArray *allItems;
@property(nonatomic,readonly)NSArray *itemsUnder50;
@property(nonatomic,readonly)NSArray *itemsAbove50;

+(instancetype)shareStore;
-(BNRItem *)creatItem;
-(NSArray *)creatItems;

@end[/code]

Use “-(nsarray *)createItems” to replace “-(bnritem *)createItem”.

BNRItemStore.m

[code]#import “BNRItemStore.h”
#import “BNRItem.h”

@interface BNRItemStore()
@property(nonatomic)NSMutableArray *privateItems;
@property(nonatomic)NSMutableArray *privateItemsUnder50;
@property(nonatomic)NSMutableArray *privateItemsAbove50;

@end

@implementation BNRItemStore

#pragma mark - Singleton creat

+(instancetype)shareStore
{
static BNRItemStore *shareStore = nil;
if (!shareStore) {
shareStore = [[self alloc]initPrivate];

}
return shareStore;

}

-(instancetype)init
{
@throw [NSException exceptionWithName:@“Singleton” reason:@“Use +[BNRItemShare shareStore]” userInfo:nil];
return nil;
}

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

-(NSArray *)allItems{
return self.privateItems;
}

-(NSArray *)itemsAbove50
{
return self.privateItemsAbove50;
}

-(NSArray *)itemsUnder50
{
return self.privateItemsUnder50;
}

-(NSArray *)creatItems
{
for (int i = 1;i<10; i++) {
BNRItem *item = [BNRItem randomItem];
[self.privateItems addObject:item];
if (item.valueInDollars < 50) {
[self.privateItemsUnder50 addObject:item];
}
else
{
[self.privateItemsAbove50 addObject:item];
}

}
[self.privateItemsAbove50 addObject:@"NO more item"];
[self.privateItemsUnder50 addObject:@"NO more item"];
return self.privateItems;

}

@end[/code]

And initialize in BNRItemsViewController
BNRItemsViewController.m

[code]#import “BNRItemsViewController.h”
#import “BNRItem.h”
#import “BNRItemStore.h”

@interface BNRItemsViewController ()

@end

@implementation BNRItemsViewController

-(instancetype)init{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
[[BNRItemStore shareStore]creatItems];
}
NSLog(@“under50:%@”,[[BNRItemStore shareStore]itemsUnder50]);
NSLog(@“Above50:%@”,[[BNRItemStore shareStore]itemsAbove50]);

return self;

}

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

  • (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@“UITableViewCell”];
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

#pragma mark - Table view data source

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@“UITableViewCell” forIndexPath:indexPath];

NSArray *itemAbove50 = [[BNRItemStore shareStore]itemsAbove50];
NSArray *itemUnder50= [[BNRItemStore shareStore]itemsUnder50];
if (indexPath.section ==0) {
    BNRItem *item1 = itemAbove50[indexPath.row];
    cell.textLabel.text=[item1 description];

}else{
    BNRItem *item2 = itemUnder50[indexPath.row];
    cell.textLabel.text = [item2 description];
}
return cell;

}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section ==0) {
    return [[[BNRItemStore shareStore]itemsAbove50]count];
    }else{
    return [[[BNRItemStore shareStore]itemsUnder50]count];
    }
    }

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

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @“Items over 30”;
}else{
return @“Items under 30”;
}
}

@end
[/code]