Silver Challenge Solution


#1

After a lot of reading and searching and suffering! and with some help from Stackoverflow i just made myself trough the Silver Challenge!! Here is the code! i tried to make the most possible elegant way. Any suggestions are so welcome!!!

[code]#import “JDMItemsViewController.h”
#import “JDMItemStore.h”
#import “JDMItem.h”

@interface JDMItemsViewController()

@property (nonatomic) NSMutableArray *moreThanFifty;
@property (nonatomic) NSMutableArray *lessThanFifty;
@property (nonatomic) NSArray *rowsForSections;
@property (nonatomic) NSArray *items;
@end

@implementation JDMItemsViewController

-(instancetype)init
{
//Call the superclass’s designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
for (int i = 0; i < 5; i++) {
[[JDMItemStore sharedStore] createItem];
}
}

return self;

}

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

#pragma mark - tableview datasource and delegate methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if ([self.items count] > 0) {
return 2;
}else {
return 1;
}
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSArray *headers = [NSArray array];
if ([self.items count] == 0) {
headers = @[@“No items to show”];
return [headers objectAtIndex:0];
}else {

headers = @[@"Items with value above 50",@"Items with value under 50"];
    return [headers objectAtIndex:section];
}

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self.items count] > 0) {
if (section == 0) {
self.moreThanFifty = [self overFifty:[[JDMItemStore sharedStore]allItems]];
return [self.moreThanFifty count] + 1;
}

    if (section == 1) {
        self.lessThanFifty = [self underFifty:[[JDMItemStore sharedStore]allItems]];
        return [self.lessThanFifty count] + 1;
    }
}

return 1;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“UITableViewCell”];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];

//NSArray *items = [[JDMItemStore sharedStore] allItems];

NSInteger lastSectionIndex = [tableView numberOfSections];

NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;

__unused NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];

if ([self.items count] > 0) {
    
     if (indexPath.section == 0) {
         if (indexPath.row < [self.moreThanFifty count]) {
             cell.textLabel.text = [self.moreThanFifty[indexPath.row] description];
         }else {
              cell.textLabel.text = @"No more Items!";
         }
        
    
     }
     if (indexPath.section == 1) {
         if (indexPath.row < [self.lessThanFifty count]) {
             cell.textLabel.text = [self.lessThanFifty[indexPath.row] description];
         }else {
             cell.textLabel.text = @"No more Items!";
         }
        
    }
    
}

else if([self.items count] == 0)  {
    cell.textLabel.text = @"No more Items!";
}

return cell;

}

-(void)viewDidLoad
{
[super viewDidLoad];
self.items = [[JDMItemStore sharedStore] allItems];
self.moreThanFifty = [NSMutableArray array];
self.lessThanFifty = [NSMutableArray array];
self.rowsForSections = @[self.moreThanFifty,self.lessThanFifty];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];

}

#pragma mark - helper methods

-(NSMutableArray *)overFifty:(NSArray *)items
{
if ([items count] > 0) {
for (JDMItem * i in items) {
if (i.valueInDollars >= 50) {
[self.moreThanFifty addObject:i];
}
}

}

return self.moreThanFifty;

}

-(NSMutableArray *)underFifty:(NSArray *)items
{
if ([items count] > 0) {
for (JDMItem *i in items) {
if (i.valueInDollars < 50) {
[self.lessThanFifty addObject:i];
}

    }
}

return  self.lessThanFifty;

}

@end
[/code]