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

时间:2009-05-15 20:04:39

标签: ios objective-c iphone uitableview cocoa-touch

为什么我的表格视图空白?在我添加“initWithStyle”

之前一切正常

这是它的样子:

alt text

以下是我的代码:

这是我的.h:

#import <UIKit/UIKit.h>


@interface TableViewController : UITableViewController {

NSMutableArray *jokes;
IBOutlet UITableView *jokeTableView; 


 }

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

@end

这是我的实施文件:

#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 个答案:

答案 0 :(得分:5)

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

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

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

P.S。您不应该在代码中调用[jokeTableView init],这将在解压缩笔尖时自动完成。

修改:由于您要从初始化代码设置表格视图的属性,因此绝对应该在awakeFromNib:,而不是initWithCoder:initWithStyle:

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

答案 1 :(得分:2)

initWithStyleinitWithCoder不同。您的构造函数可能甚至没有被调用。

答案 2 :(得分:2)

我不知道这是否是您问题的原因,但[jokeTableView init]中的随机awakeFromNib肯定是错误的,可能会导致这样的问题。永远不应该在没有附加init *的情况下调用alloc,在这种情况下我怀疑是否需要,因为表视图可能已经在别处创建了。

(*显然,这个规则的一个例外是当你重写init方法并调用super时。)

答案 3 :(得分:1)

好吧,如果您使用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'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];

答案 4 :(得分:0)

摆脱这个:

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

您已在initWithStyle:

中进行初始化

答案 5 :(得分:0)

我认为主要问题是您需要替换:

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

(和结束括号)

- (void)viewDidLoad {

此外,您不必使用nil结束NSMutableArray。

相关问题