可可:为什么我的表视图为空? [英] Cocoa: Why is my table view blank?

查看:95
本文介绍了可可:为什么我的表视图为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的表格视图为空白?这是很好,直到我添加了initWithStyle



这是它的外观:





这里是我的代码看起来像: p>

这是我的.h:

  #import< UIKit / UIKit .h> 


@interface TableViewController:UITableViewController {

NSMutableArray * jokes;
IBOutlet UITableView * jokeTableView;


}

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

@end

这里是我的实现文件:

  #importTableViewController.h
#importJoke.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:@如果你有五美元,Chuck Norris有五美元,Chuck Norris有比你更多的钱],
Joke jokeWithValue:@Chuck Norris的计算机上没有'ctrl'按钮,Chuck Norris总是在控制。],
[Joke jokeWithValue:@Apple每天收听Chuck Norris 99美分歌曲。],
[笑话jokeWithValue:@Chuck Norris可以打开他的眼睛打喷嚏。],
nil];

self.jokeTableView.rowHeight = 30.0;
// self.jokeTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.jokeTableView.sectionHeaderHeight = 0;
}
return self;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {//说明需要多少节(就像在地址中, name)
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [jokes count]



- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CellIdentifier = @球队;
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


解决方案>

如果你的视图控制器是从nib加载的,你需要重载 initWithCoder:方法,而不是 initWithStyle: code>。然而,正常的解决方案是在awakeFromNib:方法中添加所有自定义初始化代码,这是在加载nib并连接所有插座后调用的,因此,



请参阅 Apple文档 awakeFromNib: 有关从nibs实例化对象的详细信息。此外,如果您可以访问3.0测试版文档,请搜索 awakeFromNib:,更新的字词将更好地说明调用的方法和时间。



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



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



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


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

Here is what it looks like:

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

解决方案

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.

这篇关于可可:为什么我的表视图为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆