Challenge Solutions - Suggestions for improvements?


#1

Here’s the solutions I came up with for the challenges. It doesn’t seem the most elegant of solutions, but gets the job done.

I created two separate arrays for items of and over $50 in value and one for below.

Any comments for improvements are welcome.

#import "ItemsViewController.h"
#import "BNRItemStore.h"
#import "BNRItem.h"

@implementation ItemsViewController

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

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

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"Items Over $50";
    } else
        return @"Items Under $50";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return [[[BNRItemStore sharedStore] items50DollarsOrOver] count] + 1;
    } else
        return [[[BNRItemStore sharedStore] itemsUnder50Dollars] count] + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 44;
    
    if (([indexPath section] == 0) && ([indexPath row] < [[[BNRItemStore sharedStore] items50DollarsOrOver] count])) {
        height = 60;
    }
    
    if (([indexPath section] == 1) && ([indexPath row] < [[[BNRItemStore sharedStore] itemsUnder50Dollars] count])) {
        height = 60;
    }
    
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Set the table background
    UIImage *backgroundImage = [UIImage imageNamed:@"background.png"];
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
    
    [tableView setBackgroundView:backgroundImageView];
    
    // Check for a reusable cell first, use that if it exists
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    
    // If there is no reusable cell of this type, create a new one
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    
    // 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
    
    BNRItem *p;
    
    if ([indexPath section] == 0) {
        if ([indexPath row] < [[[BNRItemStore sharedStore] items50DollarsOrOver] count]) {
            p = [[[BNRItemStore sharedStore] items50DollarsOrOver] objectAtIndex:[indexPath row]];
        }
    } else {
        if ([indexPath row] < [[[BNRItemStore sharedStore] itemsUnder50Dollars] count]) {
            p = [[[BNRItemStore sharedStore] itemsUnder50Dollars] objectAtIndex:[indexPath row]];
        }
    }
    
    if (p) {
        [[cell textLabel] setFont:[UIFont systemFontOfSize:20]];
        [[cell textLabel] setText:[p description]];
    } else {
        // Set the font to the standard system size
        [[cell textLabel] setText:@"No more items!"];
    }
    
    
    return cell;
}

@end

#2

Hi,

I would suggest that you set the background in viewDidLoad, instead of current implementation. No point in loading the image etc for each row.

-(void)viewDidLoad { UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.jpg"]]; UITableView * tv = [self tableView]; [tv setBackgroundView:iv]; }


#3

Thank you, that’s a great tip and something I need to watch for in the future when creating cells. Certainly makes sense to breakout anything that doesn’t need to be done in the cell creation.