Bronze & Silver Challenge Solution


#1

BNRItemsViewController.m:
#import “BNRItemsViewController.h”
#import “BNRItemStore.h”
#import “BNRItem.h”
@implementation BNRItemsViewController

-(instancetype)init
{
// Call the superclass’s designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
for (int i = 0; i < 5; i++) {
[[BNRItemStore sharedStore]createItem];
}
}
return self;
}

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

-(void)viewDidLoad
{
[super viewDidLoad];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];

}

#pragma mark UITableViewDataSource protocol methods

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
// NSLog(@“Poop”);
//NSLog(@“More than fifty: %ld”,[[[BNRItemStore sharedStore]arrayOfItemWorthMoreThanFifty] count] );
return [[[BNRItemStore sharedStore]arrayOfItemWorthMoreThanFifty] count];
}
if (section == 1) {
// NSLog(@“Poop”);
// NSLog(@“The rest: %ld”, [[[BNRItemStore sharedStore]arrayOfItemsWorthLessThanFifty] count]);
return [[[BNRItemStore sharedStore]arrayOfItemsWorthLessThanFifty] count] + 1;
}
return 5;
}

  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
    if (section == 0) {
    return @“More than $50”;
    }
    if (section == 1) {
    return @“The rest”;
    }
    return @"";
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // Get a new or recycled cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“UITableViewCell” forIndexPath:indexPath];
    //NSLog(@“section : %ld, row: %ld”, (long)indexPath.section, (long)indexPath.row) ;

    // 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
    if (indexPath.section == 0) {
    NSArray *items = [[BNRItemStore sharedStore]arrayOfItemWorthMoreThanFifty];
    BNRItem *item = items[indexPath.row];
    cell.textLabel.text = [item description];
    return cell;
    }
    if (indexPath.section == 1) {
    NSArray *items = [[BNRItemStore sharedStore]arrayOfItemsWorthLessThanFifty];
    if (indexPath.row == [[[BNRItemStore sharedStore]arrayOfItemsWorthLessThanFifty] count]) {
    cell.textLabel.text = @“No more items!”;
    return cell;
    }
    BNRItem *item = items[indexPath.row];
    cell.textLabel.text = [item description];
    return cell;
    }
    cell.textLabel.text = @“No more items!”;

    return cell;
    }
    @end


BNRItem.h:
#import <Foundation/Foundation.h>

@class BNRItem;

@interface BNRItemStore : NSObject

@property (nonatomic,readonly,copy) NSArray *allItems;
@property (nonatomic,readonly,copy) NSArray *sortedItems;
@property (nonatomic, readonly,copy) NSArray *arrayOfItemWorthMoreThanFifty;
@property (nonatomic, readonly,copy) NSArray *arrayOfItemsWorthLessThanFifty;

// Notice that this is a class method and prefixed with a + instad of a -
+(instancetype)sharedStore;
-(BNRItem *)createItem;

@end


BNRItemStore.m:
-(NSArray *)arrayOfItemWorthMoreThanFifty{
NSSortDescriptor *fristDescriptor = [[NSSortDescriptor alloc]initWithKey:@“valueInDollars” ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:fristDescriptor];
NSArray *sortedArray = [[self allItems]sortedArrayUsingDescriptors:sortDescriptors];
NSMutableArray *itemFifty = [[NSMutableArray alloc]init];
for(BNRItem *item in sortedArray)
{
if (item.valueInDollars > 50.0) {
[itemFifty addObject:item];
}
}
return itemFifty;
}
-(NSArray *)arrayOfItemsWorthLessThanFifty{
NSSortDescriptor *fristDescriptor = [[NSSortDescriptor alloc]initWithKey:@“valueInDollars” ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:fristDescriptor];
NSArray *sortedArray = [[self allItems]sortedArrayUsingDescriptors:sortDescriptors];
NSMutableArray *itemFifty = [[NSMutableArray alloc]init];
for(BNRItem *item in sortedArray)
{
if (item.valueInDollars <= 50.0) {
[itemFifty addObject:item];
}
}
return itemFifty;
}

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

  • (BNRItem *)createItem
    {
    BNRItem *item = [BNRItem randomItem];

    [self.privateItems addObject:item];

    return item;
    }

Hope this helps