无法以编程方式添加UITableView

时间:2013-11-13 22:53:08

标签: ios iphone objective-c xcode uitableview

我最近将.xib文件转换为Storyboards,并决定以编程方式将UITableView添加到故事板中的ViewController

我使用此代码来完成此任务:

// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

    UITableView *table;
}



// ViewController.m
#import "ViewController.h"

@interface ViewController (PrivateStuff)
// unimportant code...
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    table = [[UITableView alloc] initWithFrame:self.view.bounds];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
}

不幸的是,每当我尝试运行它时,Xcode都会给我这个消息......

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "ViewController" nib but didn't get a UITableView.'

......这没有任何意义。我有一些需要连接到正确顺序的所有内容,而UITableView的所有其他视图都没有问题,尽管代码是相同的。

我该怎么做才能解决这个问题?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我同意@ rdelmar-Xcode说在[tableviewcontroller loadView]调用期间加载了一个nib,这意味着代码中的某个地方有一个tableviewcontroller ...但它也意味着你创建的nib不知何故仍然链接到您的视图控制器。您可能需要查看Xcode中的实用程序下的连接检查器,看看您的nib是否仍然存在任何持久链接并将其删除。否则,只要您在cellForRowAtIndexPath方法中提供自定义或通用的TableViewCell,上述代码就可以正常工作