【问题标题】:Cocoa: Why is my table view blank?Cocoa:为什么我的表格视图是空白的?
【发布时间】:2010-10-26 14:50:06
【问题描述】:

为什么我的表格视图是空白的?在我添加“initWithStyle”之前一直很好

这是它的样子:

这是我的代码的样子:

这是我的 .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

【问题讨论】:

  • 我不认为你需要一个属性jokeTableView。只保留 IBOutlet ... 删除:@property (retain) UITableView *jokeTableView;

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


【解决方案1】:

如果您的视图控制器是从 nib 加载的,您需要覆盖 initWithCoder: 方法,而不是 initWithStyle:。

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

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

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 我同意,TableView 不必在每次可见时都进行初始化。我也不认为这是填充数据的问题,因为您至少会看到空白的 TableViewCells。
      【解决方案4】:

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

      【讨论】:

      • 我更改了代码并在 initWithStyle 中对其进行了初始化。还是一样的问题
      • 你需要显示初始化这个控制器的代码,从显示的内容我看不出这个代码是如何被调用的。
      【解决方案5】:

      摆脱这个:

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

      您已经在 initWithStyle: 中进行初始化

      【讨论】:

        【解决方案6】:

        我认为主要问题是你需要更换:

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

        (和右括号)

        与

        - (void)viewDidLoad {
        

        另外,你不应该用 nil 结束 NSMutableArray。

        【讨论】:

        • 你需要以 nil 结束 NSMutableArray 参见developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/…
        • 另一部分似乎也不正确,每次从 xib 取消归档视图时都会调用 viewDidLoad(如果它存储在 xib 中),因此您每次都会重新初始化笑话,这不是您想要的。当内存不足并稍后重新加载时,视图可以被卸载(通过控制器?),我认为 viewDidLoad 每次都会被调用。
        • @stefan 你的权利。但是我按照你说的做了,现在电视显示出来了,但它不会显示...怎么了?
        • 你是对的 NSMutableArray——我一直使用 addObject 来填充我的数组,它不会接受 nil。每次调用 viewDidLoad 时重新初始化数组的问题可以通过我首先检查 self.jokes 中的任何内容来解决。
        猜你喜欢
        • 2017-03-04
        • 2012-12-08
        • 1970-01-01
        • 2012-02-01
        • 2011-09-12
        • 2014-07-20
        • 1970-01-01
        • 2015-07-30
        • 1970-01-01
        相关资源
        最近更新 更多