Challenge Help


#1

Hey guys, I was able to figure out how to have 2 sections and how to give those titles, but i’m not able to figure how to place Possession objects in group1/group2. any help is greatly appreciated. here’s my code so far.

ItemsViewController.h

#import "ItemsViewController.h"
#import "PossessionStore.h"
#import "Possession.h"

@implementation ItemsViewController

-(id)init
{
    // call the superclass's designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];
    
    if(self)
    {
        for(int i = 0;i < 10; i++)
        {
            [[PossessionStore defaultStore] createPossession];
        }
    }
    
    return self;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [[[PossessionStore defaultStore] allPossessions] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // check for a reusable cell first, use that if it exists
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    
    // if there is no reusable cell of this type, create a new one
    if (!cell) {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault 
                 reuseIdentifier:@"UITableViewCell"] autorelease];
    }
    
    // set the text on the cell with the description of the possession
    // that is at the nth index of possessions, where n = row this cell
    // will appear in on the tableview
    Possession *p = [[[PossessionStore defaultStore] allPossessions] objectAtIndex:[indexPath row]];
    
    // This is where i'm trying to figure out how to place in group1 or group2, but how!?!?
    
    if([p valueInDollars]>50.00)
    {
        [[cell textLabel] setText:[p description]];
    }else
    {
    
        [[cell textLabel] setText:[p description]];
        
    }
    
    return cell;
    
}

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    
    switch (section) {
        case 0:
            return @"Title1";
            break;
            
        case 1:
            return @"Title2";
            break;
            
        default:
            return @"test";
            break;
    }
    
    
    
}
@end

#2

Here is a hint to get you started. Right now, you have one array (the allPossessions array in the PossessionStore). Ideally, you should have a separate array for each “set” of Possessions.

There are an infinite number of ways you could accomplish this. One way is to separate it out within the PossessionStore itself. So the createPossession method could do the check, and if the value is >50, it would add it to one array, else it would add it to the other array. Now you have two arrays (so instead of, or potentially in addition to, the allPossessions array, you’d have a cheapPossessions array and an expensivePossession array).

Although that way will work, I’d recommend having the separate arrays not within the PossessionStore, but within the table view methods. An elegant, but challenging, solution (and the one that I would use if solving this same problem) would be to use the filteredArrayUsingPredicate: method on NSArray. Take a look at the documentation on NSArray and NSPredicate to see how to use these.


#3

Here is how I did. It is crude, not elegant, but it works.

==================== ItemsViewController.h =====================
#import <Foundation/Foundation.h>

@interface ItemsViewController : UITableViewController
{
NSMutableArray *under50;
NSMutableArray *over50;
}
@end

===================== ItemsViewController.m ===================

@implementation ItemsViewController

  • (id)init
    {
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
    // Initialization code here.
    for (int i = 0; i< 10; i++) {
    [[PossessionStore defaultStore] createPossession];
    }
    }

    over50 = [[NSMutableArray alloc] init];
    under50 = [[NSMutableArray alloc] init];

    for (Possession *p in [[PossessionStore defaultStore] allPossessions]){
    if ([p valueInDollars] >= 50) {
    [over50 addObject:p];
    }
    else
    [under50 addObject:p];
    }

    return self;
    }

  • (id)initWithStyle:(UITableViewStyle)style
    {
    return [self init];
    }

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

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {

    if (section == 0)
    return [under50 count];
    else
    return [over50 count];
    }

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“UITableViewCell”];
    if (!cell) {

      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease];
    

    }

    if ([indexPath section] == 0){
    Possession *p = [under50 objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[p description]];
    [[cell textLabel] setFont:[UIFont systemFontOfSize:13.0] ];

    }
    else {
    Possession *p = [over50 objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[p description]];
    [[cell textLabel] setFont:[UIFont systemFontOfSize:14.0] ];

    }

    return cell;
    }

  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
    if (section == 0){
    return @“Items Under $50”;
    }

    else {

      return @"Items Over $50";
    

    }
    }

  • (void)dealloc
    {
    [over50 release];
    [under50 release];
    [super dealloc];

}

@end