gpt4 book ai didi

ios - cocoa : Why is my table view blank?

转载 作者:可可西里 更新时间:2023-11-01 06:22:37 26 4
gpt4 key购买 nike

为什么我的 TableView 是空白的?在我添加“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

最佳答案

如果您的 View Controller 是从 nib 加载的,您将需要覆盖 initWithCoder: 方法,而不是 initWithStyle:

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

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

附言您不应该在代码中调用 [jokeTableView init],这将在解压 nib 时自动完成。

编辑:由于您是通过初始化代码为表格 View 设置属性,因此这绝对应该在 awakeFromNib: 中,而不是 initWithCoder:initWithStyle:

查看屏幕截图,您可能还想使用 Interface Builder 确保 UITableView 已正确放置在 View Controller 的 nib 文件中,并且创建 View Controller 的代码(或 nib)指的是正确的 nib 文件 - 如果它只是未加载的 TableView 的数据,您会在空单元格之间看到灰线,而不仅仅是像该图像中那样的空白区域。

关于ios - cocoa : Why is my table view blank?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/870627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com