Silver challenge problem


#1

all was ok until i tried to add extra row for end of second section to allow “no more item” text

now getting NSrange exceptions.

I had already successfully created two arrays for bronze challenge.

I am not sure how to enter code in forum?

However i think my problem lies in my cellForRowAtIndexPath method.


#2

Sorry I had failed to add code.

@implementation BNRItemsViewController

-(instancetype) init
{
////call superclasses designated initialiser
self = [super initWithStyle:UITableViewStylePlain];
////update to add five items to BNRItemStore

if (self)
{
    for (int i = 0; i <5; i++)
    {
        [[BNRItemStore sharedStore] createItem]; //remember class method!
    }
}


return  self;

}
-(instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
////ensures all instances will use UITableViewStylePlain]

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

  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    //add titles for each section

{
if (section == 0)
{
return @“Cheap Items”;
}

else
{
 return @"Expensive Items";
}

}

////following 2 required methods for adopting UITableViewDataSource protocol

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    if (section == 0)
    {
    return [[[BNRItemStore sharedStore] cheapItems] count];
    }
    else
    //add row for no more items cell

    {
    return ([[[BNRItemStore sharedStore] expensiveItems] count] + 1);
    }

}

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

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

    NSArray *expensiveItems = [[BNRItemStore sharedStore] expensiveItems];
    BNRItem *eItem = expensiveItems[indexPath.row];
    NSInteger sum = expensiveItems.count;

    if (indexPath.section == 0)
    {
    NSArray *cheapItems = [[BNRItemStore sharedStore] cheapItems];
    BNRItem *cItem = cheapItems[indexPath.row];
    cell.textLabel.text = [cItem description];
    }

    //get last row in second section for no more item cell
    else if (indexPath.row == sum)
    {
    cell.textLabel.text = @“No more Items”;
    }

    else
    {
    cell.textLabel.text = eItem.description;
    }

    return cell;

}

////to use above modification need to overide viewDisLoad to register
////UITableViewCell class with the tableView

-(void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@“UITableViewCell”];
}

@end