My Bronze Solution


#1

This Bronze Challenge really worked me but I guess that’s what it’s all about. Took me about a week of re programming and researching and I finally got it to work. I’ll try to break down my code…

After weeks of trying different method (making multiple array properties for BNRItemViewController…) I re read the chapter and came across where it said BNRItemStore would be a SINGLETON. So a bell went off and I realized that one array is initialized in the view controller and the same items from the store would be re used. So every time I would call the array from the store I would get the same items. I then took those items and split them into 2 mutable arrays one for items over $50 and one for items under $50. I then added them to the table view in the specified sections.

Here is BNRItemsViewController.m

I imported the header for the classes I would use and I have no private properties for the view controller.

when the view’s init method is called it gets 5 random items from BNRItemStore and these items will be reused because BNRItemStore is a Singleton…

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

@interface BNRItemsViewController ()

@end

@implementation BNRItemsViewController


// initializer for table view controller
-(instancetype)init
{
    // Call superclass's designated initilizer
    self = [super initWithStyle:UITableViewStylePlain];
    
    // creates an array of 5 random items
    if (self) {
        for (int i = 0; i < 5; i++) {
            [[BNRItemStore sharedStore] createItem];
        }
    }
    return self;
}

I then set the table view style

// set the style of the table view
-(instancetype)initWithStyle:(UITableViewStyle)style
{
    return [self init];
}

I set the origins of the table view so it wouldn’t overlap with status bar at top of window

// sets the origin of table in window
-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.contentInset = UIEdgeInsetsMake(20.0f, 0.0f, 0.0f, 0.0f);
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}

gets the number of rows in each section

// returns the number of rows in each section
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    NSArray *items = [[BNRItemStore sharedStore]allItems];
    NSMutableArray *itemsOver50 = [self itemsOver50:items];
    NSMutableArray *itemsUnder50 = [self itemsUnder50:items];
    
    
    if (section == 0){
        return [itemsUnder50 count];
    }
        else {
            return [itemsOver50  count];
        }
}

methods to get mutable arrays of items over 50 and items under 50

// gets a mutable array of items that have a value over 50
-(NSMutableArray *)itemsOver50: (NSArray *)allItems
{
    NSMutableArray *over50Items = [[NSMutableArray alloc] init];
    
    for (BNRItem *item in allItems) {
        if (item.valueInDollars >= 50){
            [over50Items addObject:item];
        }
    }
    
    return over50Items;
}

// gets a mutable array of items that have a value over 50
-(NSMutableArray *)itemsUnder50: (NSArray *)allItems
{
    NSMutableArray *itemsUnder50 = [[NSMutableArray alloc] init];
    
    for (BNRItem *item in allItems) {
        if (item.valueInDollars < 50){
            [itemsUnder50 addObject:item];
        }
    }
    
    return itemsUnder50;
}

// method to setup the cells that display the items in their section

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // Get a new or recycled cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];

    
    NSArray *items = [[BNRItemStore sharedStore]allItems];
    NSMutableArray *itemsOver50 = [self itemsOver50:items];
    NSMutableArray *itemsUnder50 = [self itemsUnder50:items];

    
    if (indexPath.section == 0) {

        cell.textLabel.text = [itemsUnder50[indexPath.row] description];
     
    } else if (indexPath.section == 1){
        cell.textLabel.text = [itemsOver50[indexPath.row] description];
      
    }
  
    return cell;
}

sets up the title for the section headers

// sets the title for the section headers
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        return @"Items Under $50";
    }  else {
        return @"Items $50 and Over";
    }
}

returns the number of sections

// sets the number of sections in table view
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

Feel free to take a look and make suggestions…