Gold Silver and Bronze challenge solution using NSpredicate

#1

[code]//
// BNRItemsViewController.m
// Homepwner
//
// Created by Vaasu on 9/18/15.
//

#import “BNRItemsViewController.h”
#import “BNRItems.h”
#import “BNRItemStore.h”

@implementation BNRItemsViewController

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

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

-(void)viewDidLoad
{
[super viewDidLoad];
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@“logo.png”]];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@“UITableViewCell”];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSPredicate *predicate;
if (section == 0) {
predicate = [NSPredicate predicateWithFormat:@“SELF.valueInDollar < 50”];
}
else if (section == 1){
predicate = [NSPredicate predicateWithFormat:@“SELF.valueInDollar > 50”];
}
else if (section == 2){
return 1;
}
NSArray *arr = [[[BNRItemStore sharedStore] allItems]filteredArrayUsingPredicate:predicate];
return [arr count];

}

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“UITableViewCell” forIndexPath:indexPath];
NSPredicate *predicate;
if (indexPath.section == 0) {
predicate = [NSPredicate predicateWithFormat:@“SELF.valueInDollar < 50”];
}else if(indexPath.section == 1){
predicate = [NSPredicate predicateWithFormat:@“SELF.valueInDollar > 50”];
}
else if(indexPath.section == 2){
cell.textLabel.text = @“No More Items!”;
cell.backgroundColor = [UIColor clearColor];
return cell;
}
BNRItems *item =[[[BNRItemStore sharedStore] allItems]filteredArrayUsingPredicate:predicate][indexPath.row];
cell.textLabel.text = item.description;
cell.textLabel.font = [UIFont systemFontOfSize:20.0];
cell.backgroundColor = [UIColor clearColor];
return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height;

switch (indexPath.section) {
    case 0:
    case 1:
        height = 60.0;
        break;
        
    default:
        height = 44.0;
        break;
}

return height;

}

@end
[/code]