Challenge solutions


#1

some things to keep in mind if you’re reading this:

  1. I did not tackle making the background an image, since I didn’t expect to learn much doing that.
  2. I taught myself blocks and NSPredicate for the purposes of sorting and filtering the array of items. These are covered later, and I could have done most of it without them, but I’ve done a lot of coding in other languages that had lambdas and operations like map, so I wanted to figure out how to do something equivalent in ObjC and this was my first decent chance.
  3. In general I prefer a functional style – most importantly I try to adhere to the paradigm that a function should have a single exit point. This tends to make debugging easier, and certainly makes using a debugger more enjoyable.
  4. I considered eliminating the hardcoding of things into 3 sections, but I finally decided that it would be minimal value for significant effort.
//
//  BNRItems.m
//  Homepwner
//
//  Created by me on 3/8/14.
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

#import "BNRItemsViewController.h"
#import "BNRITemStore.h"
#import "BNRItem.h"

@implementation BNRItemsViewController

- (instancetype)init
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        for (int i = 0; i < 8; i++) {
            [[BNRItemStore sharedStore] createItem];
        }
    }
    return self;
}

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

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

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

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    NSArray *allItems = [[BNRItemStore sharedStore] allItems];
    NSArray *itemsOverFifty = [self itemsOver50];
    NSArray *itemsUnderFifty = [self itemsUnder50];
    
    int result = 1;
    if (section == 0) {
        result = [itemsOverFifty count];
    }
    if (section == 1) {
        result = [itemsUnderFifty count];
    }
    return result;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"
                                                            forIndexPath:indexPath];
    NSArray *sortedItems = [[[BNRItemStore sharedStore] allItems]
                         sortedArrayUsingComparator:^NSComparisonResult(BNRItem *obj1, BNRItem *obj2) {
        return obj1.valueInDollars < obj2.valueInDollars;
    }];
    
    NSArray *itemsOverFifty = [self itemsOver50:sortedItems];
    NSArray *itemsUnderFifty = [self itemsUnder50:sortedItems];

    BNRItem *item = nil;
    if (indexPath.section == 0) {
        item = itemsOverFifty[indexPath.row];
    }
    else if (indexPath.section == 1) {
        item = itemsUnderFifty[indexPath.row];
    }

    if (indexPath.section < 2) {
        cell.textLabel.text = [item description];
        cell.textLabel.font = [UIFont systemFontOfSize:20];
    }
    else {
        cell.textLabel.text = @"No more items!";
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat result = 44.0;
    if (indexPath.section < 2) {
        result = 60.0;
    }
    return result;
}

- (NSArray *)itemsOver50:(NSArray *)allItems
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"valueInDollars >= 50"];
    return[allItems filteredArrayUsingPredicate:predicate];
}

- (NSArray *)itemsUnder50:(NSArray *)allItems
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"valueInDollars < 50"];
    return[allItems filteredArrayUsingPredicate:predicate];
}
@end