Gold Challenge: style is messed up when scrolling cells

#1

Hello,

I had some fun with the Gold Challenge trying to change the style of the cells, especially the “No more items !” cell, that I changed to show the total number of items stored.

When I build the program, everything is fine according to what I wanted (regarding the style of the text), but when I am scrolling down and up, the cells are messed up, the row height is messed up as well as the color of the font.

Could somebody explain me why ?

Here is my code:

BNRItemStore

#import "BNRItemStore.h"
#import "BNRItem.h"

@interface BNRItemStore ()

@property (nonatomic) NSMutableArray *privateItems;
@property (nonatomic) NSMutableArray *privateItems_inf50;
@property (nonatomic) NSMutableArray *privateItems_sup50;

@end

@implementation BNRItemStore

+(instancetype)sharedStore
{
    static BNRItemStore *sharedStore = nil;
    
    //Do I need to create a sharedStore ?
    if (!sharedStore) {
        sharedStore = [[self alloc] initPrivate];
    }
    return sharedStore;
}

//If a programmer calls [[BNRItemsStore alloc] init], let him know the error of his ways
-(instancetype)init
{
    @throw [NSException exceptionWithName:@"Singleton"
                                   reason:@"Use +[BNRItemStore sharedStore]"
                                 userInfo:nil];
    return nil;
}

//Here is the real (secret) intializer
-(instancetype)initPrivate
{
    self = [super init];
    if (self) {
        //BRONZE SOLUTION
        _privateItems_inf50 = [[NSMutableArray alloc] init];
        _privateItems_sup50 = [[NSMutableArray alloc] init];
        _privateItems = [[NSMutableArray alloc] initWithObjects:_privateItems_inf50, _privateItems_sup50, nil];
        NSLog(@"_privateItems has %lu arrays", (unsigned long) [self.privateItems count]);
    }
    return self;
}

-(NSArray *)allItems
{
    return self.privateItems;
}

//BRONZE SOLUTION

-(NSArray *)allItems_inf50
{
    return self.privateItems_inf50;
}

-(NSArray *)allItems_sup50
{
    return self.privateItems_sup50;
}

-(BNRItem *)createItem
{
    BNRItem *item = [BNRItem randomItem];
    
    if (item.valueInDollars <= 50) {
        [self.privateItems_inf50 addObject:item];
        NSLog(@"_privateItems_inf50 has %lu items", (unsigned long)[self.privateItems_inf50 count]);
    } else {
    [self.privateItems_sup50 addObject:item];
        NSLog(@"_privateItems_sup50 has %lu items", (unsigned long)[self.privateItems_sup50 count]);
    }
    return item;
}

@end

BNRItemsViewController

#import "BNRItemsViewController.h"
#import "BNRItemStore.h"
#import "BNRItem.h"

@interface BNRItemsViewController()

@property (nonatomic) int numberOfItems;

@end

@implementation BNRItemsViewController


//Designated Initializer is initWithStyle  Changing to init
-(instancetype)init
{
    //ALWAYS call the superclass's designated initializer
    self = [super initWithStyle:UITableViewStylePlain];
    _numberOfItems = 12;
    if (self) {
        for (int i = 0; i < _numberOfItems; i++) {
            BNRItem *item = [[BNRItemStore sharedStore] createItem];
            if (item.valueInDollars <= 50) {
                NSLog(@"We just created the item %d: %@, at the index %lu", i, item.description, (unsigned long)[[[BNRItemStore sharedStore] allItems_inf50] indexOfObject:item]);
            } else {
                NSLog(@"We just created the item %d: %@, at the index %lu", i, item.description, (unsigned long)[[[BNRItemStore sharedStore] allItems_sup50] indexOfObject:item]);
            }
        }
        NSLog(@"allItems has %lu arrays", (unsigned long) [[[BNRItemStore sharedStore] allItems] count]);

    }
    return self;
    
}



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

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    //BRONZE CHALLENGE
    /*
    NSLog(@"We create totally %lu sections", [[[BNRItemStore sharedStore] allItems] count]);
    return [[[BNRItemStore sharedStore] allItems] count];
    */

    
    //SILVER CHALLENGE
    if ([[[BNRItemStore sharedStore] allItems] count] == 0) {
        //Even if there is no items in the store, there is one section to show that there is no items.
        NSLog(@"No items created so only 1 section");
        return 1;
    } else {
        NSLog(@"We create one more section than expected to store the ""No more items"" shit, so totally %lu", [[[BNRItemStore sharedStore] allItems] count] +1);
        return [[[BNRItemStore sharedStore] allItems] count] +1 ;
    }
}

-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
    //BRONZE CHALLENGE
    if (section == 0) {
        NSLog(@"And the number of rows sections 1: %lu", [[[BNRItemStore sharedStore] allItems_inf50] count]);
        NSLog(@"And the total number of rows in all sections is: %d", _numberOfItems);
        return [[[BNRItemStore sharedStore] allItems_inf50] count];
    } else if (section == 1) {
        NSLog(@"And the number of rows sections 1: %lu", [[[BNRItemStore sharedStore] allItems_sup50] count]);
        NSLog(@"And the total number of rows in all sections is: %d", _numberOfItems);
        return [[[BNRItemStore sharedStore] allItems_sup50] count];
    }
    //SILVER CHALLENGE
    else {
        return 1;
    }

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //BRONZE CHALLENGE
    if (section == 0) {
        return @"Items ≤ $50";
    } else if (section == 1) {
        return @"Items > $50";
    }
    //SILVER CHALLENGE
    else {
        return nil;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    /*//Create an instance of UITableViewCell, with default appearance
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                   reuseIdentifier:@"UITableViewCell"];
    */
    
    //Get a new or recycled cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"
                                                            forIndexPath:indexPath];
    /* For the lesson
    //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
    NSArray *items = [[BNRItemStore sharedStore] allItems];
    BNRItem *item = items[indexPath.row];
    NSLog(@"%@ is in Section %ld Row %ld", item.itemName, (long)[indexPath section], (long)[indexPath row]);
     */
    
    //BRONZE CHALLENGE
    NSArray *items = [[NSArray alloc] init];
    BNRItem *item = [[BNRItem alloc] init];

    if (indexPath.section == 0) {
        items = [[BNRItemStore sharedStore] allItems_inf50];
        item = items[indexPath.row];
        NSLog(@"%@ is in Section %ld Row %ld", item.itemName, (long)[indexPath section], (long)[indexPath row]);
        cell.textLabel.text = [item description];
    } else if (indexPath.section == 1) {
        items = [[BNRItemStore sharedStore] allItems_sup50];
        item = items[indexPath.row];
        NSLog(@"%@ is in Section %ld Row %ld", item.itemName, (long)[indexPath section], (long)[indexPath row]);
        cell.textLabel.text = [item description];
    }
    //SILVER CHALLENGE
    else {
        cell.textLabel.text = [[NSString alloc] initWithFormat:@"%d items", _numberOfItems];
        
        //GOLD CHALLENGE
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor grayColor];
        
        /*UIFontDescriptor *cellItalic = [cell.textLabel.font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
        cell.textLabel.font = [UIFont fontWithDescriptor:cellItalic size:0];*/
        cell.textLabel.font = [UIFont fontWithDescriptor:[cell.textLabel.font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic] size:0];
        self.tableView.rowHeight = 60;

    }
    return cell;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.contentInset = UIEdgeInsetsMake(20.0f, 0.0f, 0.0f, 0.0f);
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:@"UITableViewCell"];
    
    //GOLD CHALLENGE
    self.tableView.separatorEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    self.tableView.sectionHeaderHeight = 20;
    self.tableView.rowHeight = 44;
}


@end