Bronze Challenge Help


#1

I tried to sum up everything in a single view controller for this exercise but the thing is the when i run the project i can see table filing the screen but there’s nothing its empty.
Could there be an error in my coding?

[code]#import “BNRItemViewController.h”
#import “BNRItemStore.h”
#import “BNRItem.h”

@interface BNRItemViewController ()

@property (nonatomic, retain) NSArray *BNRItemStoreArray;
@property (nonatomic) NSMutableArray *lessThan50Product;
@property (nonatomic) NSMutableArray *moreThan50Product;

@end

@implementation BNRItemViewController

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

//Changing desginated initializer to init
-(instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];
}

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

//Get a new or recycled cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];

//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

self.BNRItemStoreArray = [[BNRItemStore sharedStore]allItems];
//BNRItem *item = self.BNRItemStoreArray[indexPath.row];
[self filterArray];

if (indexPath.section == 0) {
    BNRItem *item = self.lessThan50Product[indexPath.row];
    cell.textLabel.text = [item description];
}else{
    BNRItem *item = self.moreThan50Product[indexPath.row];
    cell.textLabel.text = [item description];
}




//cell.textLabel.text = [item description];


return cell;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section ==0) {
return [self.lessThan50Product count];
}
if (section ==1) {
return [self.moreThan50Product count];
}

return 1;

}

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

-(void)viewDidLoad
{
[super viewDidLoad];

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

}

//filter array
//Implement method to notify on how many section
//Implemnent what to show in each section

-(void)filterArray
{

for (BNRItem *h in self.BNRItemStoreArray) {
    if (h.valueInDollars<50) {
        self.lessThan50Product = [[NSMutableArray alloc]init];
        [self.lessThan50Product addObject:h ];
    }else{
        self.moreThan50Product = [[NSMutableArray alloc]init];
        [self.moreThan50Product addObject:h];
    }
}

}

@end[/code]

pls help.


#2

You have this line:

Since it is a commented out line, you aren’t putting any text into the cell. You’re just leaving it empty.


#3

[quote=“ChristianKeur”]You have this line:

Since it is a commented out line, you aren’t putting any text into the cell. You’re just leaving it empty.[/quote]

but this line is written in the if statement when the condition is met. Isn’t it right? its just above the commented lines


#4

[quote=“chanyeechoong”][quote=“ChristianKeur”]You have this line:

Since it is a commented out line, you aren’t putting any text into the cell. You’re just leaving it empty.[/quote]

but this line is written in the if statement when the condition is met. Isn’t it right? its just above the commented lines[/quote]

Ah yes, you are correct. I noticed something else; in your filterArray method, you are re-instantiating the array every iteration of the for loop. This means that, for example, every time a new item that is less than $50 is found, you create a new mutable array which will destroy the existing array that might have an item in it. With your current implementation, you should factor out the instantiation of those arrays:

    self.lessThan50Product = [[NSMutableArray alloc]init];
    self.moreThan50Product = [[NSMutableArray alloc]init];
    
    for (BNRItem *h in self.BNRItemStoreArray) {
        if (h.valueInDollars<50) {
            [self.lessThan50Product addObject:h ];
        }else{
            [self.moreThan50Product addObject:h];
        }
    }