将故事板上的UITableView添加到ViewController时出错

时间:2013-01-05 03:04:06

标签: objective-c ios uitableview

在我的storyboard我有一个ViewController,我想添加一个UITableView,其中包含1个部分和2个静态单元格。这就是我现在所做的一切,但在编译时我遇到了这个错误。

Static table views are only valid when embedded in UITableViewController instances

我错过了什么?

2 个答案:

答案 0 :(得分:1)

看看This question。基本上是一样的。

还有其他问题,请问! :)

编辑过的东西:

    UITableViewController *controller = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[self.view addSubview:controller.tableView];

然后你需要做的就是在头文件中添加UITableViewDataSource,你需要数据源方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if([indexPath row] = 0){
do something
}else if([indexPath row] = 1){
do something else
}

答案 1 :(得分:0)

我认为您只需要在.h文件中设置tableView的数据源和委托方法

@interface viewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

然后将其链接到storyboard中的tableView。

如果有效,请通知..