Bronze, Silver & Gold Challenge


#1

ItemsViewController.m

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

@interface ItemsViewController ()
@property (nonatomic, strong) NSMutableArray *copiedArray;
@property (nonatomic, strong)NSMutableArray *numberOfRowsPerSection;
@end

@implementation ItemsViewController

  • (id)init {
    // Call the superclass’s designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];

    if (self) {
    for (int i = 0; i < 24; i++) {
    [[BNRItemStore sharedStore] createItem];
    }
    }

    [self setCopiedArray:[[[BNRItemStore sharedStore] allItems] mutableCopy]];

    [self setNumberOfRowsPerSection:[NSMutableArray arrayWithCapacity:2]];

    // Use background.jpg for non-retina display and background@2x for retina display
    [[self tableView] setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@“background.jpg”]]];

    return self;
    }

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

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    int numberOfRows = 0;

    for (int i = 0; i < [[[BNRItemStore sharedStore] allItems] count]; i++) {
    switch (section) {
    case 0:
    if ([[[[BNRItemStore sharedStore] allItems] objectAtIndex:i] valueInDollars] <= 50) {
    numberOfRows++;
    }

              break;
              
          default:
              if ([[[[BNRItemStore sharedStore] allItems] objectAtIndex:i] valueInDollars] > 50) {
                  numberOfRows++;
              }
              break;
      }
    

    }

    // Adding a row at then end of the section (Silver Challenge)
    numberOfRows++;

    // Save numberOfRows in numberOfRowsPerSection
    [[self numberOfRowsPerSection] addObject:[NSNumber numberWithInt:numberOfRows]];

    return numberOfRows;
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BOOL notFound = TRUE;
    int i = 0;
    int posRow = 0;
    BNRItem *p;

    // If this is not the last row of section
    if ([indexPath row] < [tableView numberOfRowsInSection:[indexPath section]] - 1) {
    switch ([indexPath section]) {
    case 0:
    while (notFound && i < [[self copiedArray] count]) {
    if ([[[self copiedArray] objectAtIndex:i] valueInDollars] <= 50) {
    if ([indexPath row] == posRow) {
    notFound = FALSE;

                          p = [[self copiedArray] objectAtIndex:i];
                      } else {
                          posRow++;
                      }
                  }
                  
                  i++;
              }
              
              break;
              
          default:
              while (notFound && i < [[self copiedArray] count]) {
                  if ([[[self copiedArray] objectAtIndex:i] valueInDollars] > 50) {                        
                      if ([indexPath row] == posRow) {
                          notFound = FALSE;
                          
                          p = [[self copiedArray] objectAtIndex:i];
                      } else {
                          posRow++;
                      }
                  }
                  
                  i++;
              }
              
              break;
      }
      
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
      
      if (!cell) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                        reuseIdentifier:@"UITableViewCell"];
          
          [cell setFrame:CGRectMake(0, 0, [cell frame].size.width, 600)];
      }
      
      [[cell textLabel] setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
      [[cell textLabel] setText:[p description]];
      
      return cell;
    

    } else {
    UITableViewCell *lastCell = [tableView dequeueReusableCellWithIdentifier:@“UITableViewLastCell”];

      if (!lastCell) {
          lastCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                        reuseIdentifier:@"UITableViewLastCell"];
          
          [lastCell setFrame:CGRectMake(0, 0, [lastCell frame].size.width, 600)];
      }
      
      [[lastCell textLabel] setText:@"No more items!"];
      
      return lastCell;
    

    }
    }

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // If this is not the last row of section
    if ([indexPath row] < [[[self numberOfRowsPerSection] lastObject] intValue] - 1) {
    return 60;
    } else {
    return 44;
    }
    }

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

@end[/code]