Cocoa: Why does my table look empty?

Why is my table not displayed? It was perfect until I added "initWithStyle"

Here's what it looks like:

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 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
+3
source share
6 answers

If your view controller loads from the tip, you need to override the method initWithCoder:, not initWithStyle:.

However, the usual solution would be to add all of your custom initialization code to the awakeFromNib method: it is called after loading the nib and plugging in all the sockets, so it is great for any initialization you need.

nibs . Apple docs awakeFromNib:. , - 3.0, awakeFromNib:, , .

P.S. [jokeTableView init] , , nib .

:. , awakeFromNib:, initWithCoder: initWithStyle:.

, , Interface Builder UITableView nib ( nib), , nib - , , , , .

+5

initWithStyle initWithCoder. , .

+2

, , [jokeTableView init] awakeFromNib, , . init alloc *, , , .

(* , , , init .)

+2

, initWithStyle, initWithCoder, .

self.jokes initWithCoder. viewDidLoad initWithStyle

// 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 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];
+1

:

- (void)awakeFromNib {
[jokeTableView init];
}

initWithStyle:

0

, , :

- (id)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithStyle:style]) {

( )

- (void)viewDidLoad {

, NSMutableArray nil.

0

Source: https://habr.com/ru/post/1708453/


All Articles