initWithStyle
is not the same as initWithCoder
. Your constructor probably isn't even getting called.
Well, if you are initializing this controller using initWithStyle
and you previously were calling initWithCoder
, that is your problem.
You are loading your self.jokes
in initWithCoder
. You should move that code to viewDidLoad
or to your new initWithStyle
method
// move this
self.jokes = [NSMutableArray arrayWithObjects:
[Joke jokeWithValue:@"If you have five dollars and Chuck Norris has five dollars, Chuck Norris has more money than you"],
[Joke jokeWithValue:@"There is no 'ctrl' button on Chuck Norris's computer. Chuck Norris is always in control."],
[Joke jokeWithValue:@"Apple pays Chuck Norris 99 cents every time he listens to a song."],
[Joke jokeWithValue:@"Chuck Norris can sneeze with his eyes open."],
nil];
I don't know if it's the cause of your problem, but that random [jokeTableView init]
in awakeFromNib
is certainly wrong and could cause problems like this. init
should never be called without an attached alloc
*, and in this case I doubt either is needed since the table view has presumably already been created elsewhere.
(* The one exception to this rule, obviously, is when you're overriding an init method and calling super.)
Get rid of this:
- (void)awakeFromNib {
[jokeTableView init];
}
you're already doing initialization in initWithStyle:
I think the main problem is that you need to replace:
- (id)initWithStyle:(UITableViewStyle)style {
if (self = [super initWithStyle:style]) {
(and the closing bracket)
with
- (void)viewDidLoad {
Also, you shouldn't have to end a NSMutableArray with nil.
If your view controller is being loaded from a nib, you'll need to override the initWithCoder:
method, not initWithStyle:
.
However, the normal solution would be to add all your custom initialization code in the awakeFromNib: method, this is called after the nib is loaded and all outlets are connected, so it makes a great place to do any initialization you need.
See the Apple docs on awakeFromNib:
for details on object instantiation from nibs. Also, if you have access to the 3.0 beta docs, do a search for awakeFromNib:
, the updated wording gives a better explanation of which methods are called and when.
P.S. You shouldn't be calling [jokeTableView init]
in your code, this will be done automatically when the nib is unpacked.
Edit: Since you are setting properties for your table view from your initialization code, this should definitely be in awakeFromNib:
, not initWithCoder:
or initWithStyle:
.
Looking at the screenshot, you might also want to make sure with Interface Builder that the UITableView is placed correctly in the nib file for your view controller, and that the code (or nib) that creates your view controller is referring to the right nib file - if it were just the data for the table view that had not loaded, you would see gray lines between empty cells, not just blank white space like in that image.