Cocoa: Why is my table view blank?

Why is my table view blank? It was fine until I added “initWithStyle”

Here is what it looks like:

  • UITableView with NSFetchedResultsController Does Not Load the Second Time
  • Pass multiple parameter using cell button action
  • UITableViewCell load images and reused cells
  • UITableview with more than One Custom Cells with Swift
  • Textfield in Custom Table View Cell dissappears from initial textfield and appears in other cells textfield upon scrolling
  • UITextView text cut off when there is a large amount of text
  • alt text

    And here is what my code looks like:

    Here is my .h:

    #import <UIKit/UIKit.h>
    
    
    @interface TableViewController : UITableViewController {
    
    NSMutableArray *jokes;
    IBOutlet UITableView *jokeTableView; 
    
    
     }
    
    @property (nonatomic, retain) NSMutableArray *jokes;
    @property (retain) UITableView *jokeTableView;
    
    @end
    

    And here is my implementation file:

    #import "TableViewController.h"
    #import "Joke.h"
    
    @implementation TableViewController
    @synthesize jokes;
    @synthesize jokeTableView;
    
    - (void)awakeFromNib {
    [jokeTableView init];
    }
    
    
    
    
    - (id)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithStyle:style]) {
        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];
    
        self.jokeTableView.rowHeight = 30.0;
        // self.jokeTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.jokeTableView.sectionHeaderHeight = 0;
    }
    return self;
    }
    
    
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Saying how many sections wanted (Just     like in address, where sorts by first name)
    return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section {
    return [jokes count];
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Team";  
    UITableViewCell *cell = 
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
                 initWithFrame:CGRectZero 
                 reuseIdentifier:CellIdentifier] autorelease];
    }
    Joke *j = (Joke *)[jokes objectAtIndex:indexPath.row];
    if( j )
        cell.text = j.joke;
    return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    }
    
    
    - (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    }
    
    - (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; 
    }
    
    - (void)dealloc {
    [jokes release];
    [jokeTableView release];
    [super dealloc];
    }
    
    @end
    

    6 Solutions Collect From Internet About “Cocoa: Why is my table view blank?”

    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.

    initWithStyle is not the same as initWithCoder. Your constructor probably isn’t even getting called.

    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.)

    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];
    

    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.