Silver Challenge, Any Ideas? Hope can help me!


#1

Hi all, i have a couple of days fighting with the Silver challenge! So, i want to see if any of you folks can give me some ideas for the last row of No more items!.

Here is my code so you can check it out, i didn’t change the init methods because i think it is not an elegant solution for the challenge.

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

@interface JDMItemsViewController()

@property (nonatomic) NSMutableArray *moreThanFifty;
@property (nonatomic) NSMutableArray *lessThanFifty;
@property (nonatomic) NSInteger rowCountSectionOne;
@property (nonatomic) NSInteger rowCountSectionTwo;

@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
{
return 2;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSArray *headers = [NSArray array];
headers = @[@“Items with value above 50”,@“Items with value under 50”];

return [headers objectAtIndex:section];

}

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

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

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] - 1;

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

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

if (items > 0) {
    if (indexPath.section == 0) {
        [self overFifty:items];
        cell.textLabel.text = [self.moreThanFifty[indexPath.row] description];
    
    }
    if (indexPath.section == 1) {
        [self underFifty:items];
        cell.textLabel.text = [self.lessThanFifty[indexPath.row] description];
    }

}

else  {
    cell.textLabel.text = @"No more Items!";
}



return cell;

}

-(void)viewDidLoad
{
[super viewDidLoad];
self.moreThanFifty = [NSMutableArray array];
self.lessThanFifty = [NSMutableArray array];

[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]