【发布时间】:2012-12-19 13:00:09
【问题描述】:
在我的 storyboard 上,我有一个 ViewController,我想添加一个 UITableView,其中包含 1 个部分和 2 个静态单元格。这就是我现在正在做的所有事情,但是我在编译时遇到了这个错误。
静态表视图仅在嵌入 UITableViewController 实例时有效
我错过了什么?
【问题讨论】:
标签: objective-c ios uitableview
在我的 storyboard 上,我有一个 ViewController,我想添加一个 UITableView,其中包含 1 个部分和 2 个静态单元格。这就是我现在正在做的所有事情,但是我在编译时遇到了这个错误。
静态表视图仅在嵌入 UITableViewController 实例时有效
我错过了什么?
【问题讨论】:
标签: objective-c ios uitableview
看看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
}
【讨论】:
我认为您只需在 .h 文件中设置 tableView 的数据源和委托方法
@interface viewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
然后将其链接到情节提要中的 tableView。
如果有效请通知..
【讨论】: