Cell text label text not showing


#1

Please help me.

I can not for the life of me get the BNRItem.description text to show in my UITableViewCells when I run the program on my iphone 4s. When I run it on the ios simulator in “iPhone Retina (4 inch 64 bit)” mode, everything displays fine, all five item descriptions are listed in the UITableViewCells where they should be. then if I run it on the simulator in “iPhone Retina (4 inch)” mode, I only get four item descriptions. How does any of this make sense? My iPhone 4s is running iOS 7.1.2 and I’m obviously on mavericks on my 2009 MBP. This is the first time I’ve had any issue running the code taught in this book and for the sake of being thorough and making sure I’m not missing a stupid mistake, here are all my project files. Please do a once over and help me understand what I’m doing wrong, it’s driving me nuts. Thanks in advance! I hope someone can help me.

//
//  BNRAppDelegate.m
//  Homepwner
//
//  Created by Travis on 7/29/14.
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

#import "BNRAppDelegate.h"
#import "BNRItemsViewControlller.h"

@implementation BNRAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    BNRItemsViewControlller *itemsViewController = [[BNRItemsViewControlller alloc] init];
    // Make this instance of BNRItemsViewController the root table view controller
    self.window.rootViewController = itemsViewController;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

...other methods that are created automatically are listed here but I removed them to shorten this post

@end
//
//  BNRItemsViewControlller.h
//  Homepwner
//
//  Created by Travis Thielen on 7/29/14.
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface BNRItemsViewControlller : UITableViewController

@end

... and the BNRItemsViewController.m file...

//
//  BNRItemsViewControlller.m
//  Homepwner
//
//  Created by Travis Thielen on 7/29/14.
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

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


@implementation BNRItemsViewControlller

- (instancetype)init
{
    self = [super initWithStyle:UITableViewStylePlain];
    for (int i; i < 5; i++) {
        [[BNRItemStore sharedStore] createItem];
    }
    return self;
}

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(20, 0, 0, 0);
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return [[[BNRItemStore sharedStore] allItems] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Create an instance of UITableViewCell, with default appearance
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                   reuseIdentifier:@"UITableViewCell"];
    
    // Set the text on the cell with the description of the item
    NSArray *items = [[BNRItemStore sharedStore] allItems];
    BNRItem *item = items[indexPath.row];
    
    cell.textLabel.text = [item description];
    
    return cell;
}

@end
//
//  BNRItem.h
//  RandomItems
//
//  Created by Travis Thielen on 7/4/14.
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BNRItem : NSObject

// Class methods have a + sign at the begining of their declaration and
// either create new instances of a class or retrieve some global property
+ (instancetype)randomItem;

// Designated initializer for BNRItem
- (instancetype)initWithItemName:(NSString *)name
                  valueInDollars:(int)value
                    serialNumber:(NSString *)sNumber;

- (instancetype)initWithItemName:(NSString *)name;

@property (nonatomic, copy) NSString *itemName;
@property (nonatomic, copy) NSString *serialNumber;
@property (nonatomic) int valueInDollars;
@property (nonatomic, readonly, strong) NSDate *dateCreated;

@end
//
//  BNRItem.m
//  RandomItems
//
//  Created by Travis Thielen on 7/4/14.
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

#import "BNRItem.h"

@implementation BNRItem

+ (instancetype)randomItem
{
    // Create an immutable array of three adjectives
    NSArray *randomAdjectiveList = @[@"Shrunken", @"Rusty", @"Sticky"];
    
    // Create an immutable array of three nouns
    NSArray *randomNounList = @[@"Vaginal Discharge", @"Turducken", @"Penis-Pump"];
    
    // Get the index of a random adjective/noun from the lists
    // Note: The % operator, called the modulo operator,  gives
    // you the remainder. So adjectiveIndex is a random number
    // from 0 to 2 inclusive.
    NSInteger adjectiveIndex = arc4random() % [randomAdjectiveList count];
    NSInteger nounIndex = arc4random() % [randomNounList count];
    
    // Note that NSInteger is not an object, but a type definition
    // for "long"
    
    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                            randomAdjectiveList[adjectiveIndex],
                            randomNounList[nounIndex]];
    
    int randomValue = arc4random() % 100;
    
    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                    'O' + arc4random() % 10,
                                    'A' + arc4random() % 26,
                                    'O' + arc4random() % 10,
                                    'A' + arc4random() % 26,
                                    'O' + arc4random() % 10];
    
    BNRItem *newItem = [[self alloc] initWithItemName:randomName
                                       valueInDollars:randomValue
                                         serialNumber:randomSerialNumber];
    
    return newItem;
}

- (instancetype)initWithItemName:(NSString *)name
                  valueInDollars:(int)value
                    serialNumber:(NSString *)sNumber
{
    // Call the superclass's designated initializer
    //self = [super init];
    
    // Did the superclass's designated initializer succeed?
    // shorten the code by assigning and checking for the superclass's
    // initializer in one line
    if (self = [super init]) {
        // Give the instance variables initial values
        _itemName = name;
        _serialNumber = sNumber;
        _valueInDollars = value;
        // set _dateCreated to the current date and time
        _dateCreated = [[NSDate alloc] init];
    }
    
    // Return the address of the newly initialized object
    return self;
    
}

- (instancetype)initWithItemName:(NSString *)name
{
    // Since we already called the super init method in our designated
    // initializer, we will call that initializer and give designated values
    // for the instance variables that are not supplied. note this also takes
    // care of setting the dateCreated iVar
    return [self initWithItemName:name
                   valueInDollars:0
                     serialNumber:@""];
}

// because BNRItem inherits the init method from its superclass NSObject, if an
// instance of BNRItem were to be initialized using init it would not have any of its
// instance variables set as defined in our custom designated initializer, so we
// have to override NSObjects init method to fit our needs, giving it a generic item name
- (instancetype)init
{
    return [self initWithItemName:@"Item"];
}


- (NSString *)description
{
    NSString *descriptionString =
    [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
                                                                            self.itemName,
                                                                            self.serialNumber,
                                                                            self.valueInDollars,
                                                                            self.dateCreated];
    return descriptionString;
    
}

- (void)dealloc
{
    NSLog(@"Destroyed: %@", self);
}


@end
//
//  BNRItemStore.h
//  Homepwner
//
//  Created by Travis Thielen on 7/29/14.
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>

@class BNRItem;

@interface BNRItemStore : NSObject

@property (nonatomic, readonly) NSArray *allItems;

// Notice that this is a class method prefixed with a '+' and not a '-'
+ (instancetype)sharedStore;
- (BNRItem *)createItem;

@end
//
//  BNRItemStore.m
//  Homepwner
//
//  Created by Travis Thielen on 7/29/14.
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

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

@interface BNRItemStore ()

@property (nonatomic) NSMutableArray *privateItems;

@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 [[BNRItemStore 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) initializer
- (instancetype)initPrivate
{
    self = [super init];
    if (self) {
        _privateItems = [[NSMutableArray alloc] init];
    }
    
    return self;
}

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

- (BNRItem *)createItem
{
    BNRItem *item = [BNRItem randomItem];
    
    [self.privateItems addObject:item];
    
    return item;
}

@end

#2

Ok, just figured it out! in the enumeration used to create the five instances of BNRItem performed in the init method of my BNRViewController, I wasn’t properly setting my enumeration variable to 0. here’s what it was before:

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


@implementation BNRItemsViewControlller

- (instancetype)init
{
    self = [super initWithStyle:UITableViewStylePlain];
    for (int i; i < 5; i++) {
        [[BNRItemStore sharedStore] createItem];
    }
    return self;
}

and after setting “int i = 0” in the iteration loop, it fixed everything. amazing how such a small piece of the program can break everything.