可可:为什么我的表视图空白?

3 iphone cocoa-touch objective-c uitableview ios

为什么我的表格视图空白?在我添加"initWithStyle"之前没事

这是它的样子:

替代文字

以下是我的代码:

这是我的.h:

#import <UIKit/UIKit.h>


@interface TableViewController : UITableViewController {

NSMutableArray *jokes;
IBOutlet UITableView *jokeTableView; 


 }

@property (nonatomic, retain) NSMutableArray *jokes;
@property (retain) UITableView *jokeTableView;

@end
Run Code Online (Sandbox Code Playgroud)

这是我的实现文件:

#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
Run Code Online (Sandbox Code Playgroud)

Tob*_*hen 5

如果正在从笔尖加载视图控制器,则需要覆盖该initWithCoder:方法,而不是initWithStyle:.

但是,通常的解决方案是在awakeFromNib:方法中添加所有自定义初始化代码,这在加载nib并连接所有插座后调用,因此它是进行所需初始化的好地方.

有关从nib进行对象实例化的详细信息,请参阅Apple文档awakeFromNib:.此外,如果您可以访问3.0 beta文档,请进行搜索awakeFromNib:,更新后的措辞可以更好地解释调用哪些方法以及何时调用.

PS你不应该[jokeTableView init]在你的代码中调用,这将在解压缩nib时自动完成.

编辑:因为您要从初始化代码设置表视图的属性,所以这绝对应该是awakeFromNib:,而不是initWithCoder:initWithStyle:.

查看屏幕截图,您可能还需要确保使用Interface Builder将UITableView正确放置在视图控制器的nib文件中,并且创建视图控制器的代码(或nib)指的是正确的nib文件 - 如果它只是未加载的表视图的数据,您将看到空单元格之间的灰色线条,而不仅仅是该图像中的空白空白区域.