Bronze Challenge: Another way


#1

In BNRItemStore.m, add a new method called sectedItems

- (NSArray *)sectedItems
{
    NSMutableArray *bigItems = [[NSMutableArray alloc] init];
    NSMutableArray *otherItems = [[NSMutableArray alloc] init];
    
    for (BNRItem *item in allItems) {
        if ([item valueInDollars] > 50)
            [bigItems addObject:item];
        else
            [otherItems addObject:item];
    }
    
    [sectedItems addObject];
    [sectedItems addObject:otherItems];
    
    return sectedItems;
}

Then in ItemsViewController.m, change the code in numberOfRowsInSection into:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return [[[[ItemStore sharedStore] sectedItems] objectAtIndex:section] count];
}

Then add this


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

Then change the method cellForRowAtIndexPath into:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }
    
    BNRItem *p = [[[[ItemStore sharedStore] sectedItems] objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];
    
    [[cell textLabel] setText:[p description]];
    return cell;
}

For those who looking for less “if” :slight_smile:

It works.