My Gold Challenge Solution


#1

Here is my solution to the Gold Challenge. Please provide any insight if you would like. I basically just use Core Data features to pick out the items from assetType as Joe Conway suggested and even hinted at towards the end of the chapter. The real beauty of this solution is that I did not need to make any changes to any other files other than AssetTypePicker. BNRItemStore still is in charge of the data, AssetTypePicker just filters it for it’s own purposes. I did add a few things like some color and headers for the individual sections.

AssetTypePicker.h

#import <Foundation/Foundation.h>

@class BNRItem;

@interface AssetTypePicker : UITableViewController
{
    NSMutableArray *selectedItems;
    NSMutableArray *itemArray;
    NSInteger assetRow;
    NSArray *assetTypes;
}

@property (nonatomic, strong) BNRItem *item;
@property (nonatomic) NSInteger assetRow;
@property (nonatomic, strong) NSMutableArray *itemArray;

@end

AssetTypePicker.m

#import "AssetTypePicker.h"
#import "BNRItemStore.h"
#import "BNRItem.h"


@implementation AssetTypePicker

@synthesize item, assetRow, itemArray;

-(id)init
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

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

-(void)viewWillAppear:(BOOL)animated
{
    assetTypes = [[BNRItemStore sharedStore] allAssetTypes];
    if ([item assetType] != NULL) //This is NULL if adding a new item and asset type hasn't been set yet.
    {
        assetRow = [assetTypes indexOfObjectIdenticalTo:[item assetType]];
        
        NSSet *itemSet = [[assetTypes objectAtIndex] valueForKey:@"items"];
        itemArray = [[NSMutableArray alloc] initWithArray:[itemSet allObjects]];
    }
}

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


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rowsInSection = 0;
    
    if (section == 0)
    {
        rowsInSection = [assetTypes count];
    }
    else
    {                
        if ([itemArray count] > 0)
        {
            rowsInSection = [itemArray count];
        }
    }
    return rowsInSection;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title;
    if (section == 0)
    {
        title = @"Asset Types";
    }
    else
    {
        if ([item assetType] != NULL)
        {
            title = [@"Items in " stringByAppendingString:[[item assetType] valueForKey:@"label"]];
            title = [title stringByAppendingString:@" Category"];
        }
        else
        {
            title = @"No Category Selected.";
        }
    }
    
    return title;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)ip
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault        
                                                   reuseIdentifier:@"UITableViewCell"];
    }

    if ([ip section] == 0)
    {
        NSManagedObject *assetType = [assetTypes objectAtIndex:[ip row]];
        
        NSString *assetLabel = [assetType valueForKey:@"label"];
        [[cell textLabel] setText:assetLabel];
        [[cell textLabel] setTextColor:[UIColor blueColor]];
        
        if (assetType == [item assetType])
        {
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
            [[cell textLabel] setTextColor:[UIColor greenColor]];
            assetRow = [ip row];
        }
        else
        {
            [cell setAccessoryType:UITableViewCellAccessoryNone];
        }
    }
    else
    {        
        NSString *textLabel = [[itemArray objectAtIndex:[ip row]] itemName];
        [[cell textLabel] setText:textLabel];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)ip
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:ip];
    
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    
    NSArray *allAssets = [[BNRItemStore sharedStore] allAssetTypes];
    NSManagedObject *assetType = [allAssets objectAtIndex:[ip row]];
    [item setAssetType:assetType];
    
    [[self navigationController] popViewControllerAnimated:YES];
}
@end