【问题标题】:Error adding UITableView to ViewController on storyboard将 UITableView 添加到情节提要上的 ViewController 时出错
【发布时间】:2012-12-19 13:00:09
【问题描述】:

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

静态表视图仅在嵌入 UITableViewController 实例时有效

我错过了什么?

【问题讨论】:

    标签: objective-c ios uitableview


    【解决方案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
    }
    

    【讨论】:

    • 答案提到“将 UITableViewController 添加到您的视图中”。我可以将 UITableView 添加到视图中,但是如何将 UITableViewController 添加到视图中?
    • 嗯...猜猜你必须以编程方式添加它。改变我的答案。
    【解决方案2】:

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

    @interface viewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
    

    然后将其链接到情节提要中的 tableView。

    如果有效请通知..

    【讨论】:

    • 这难道不是静态表格视图无法做到的吗?自己没有尝试过,但这个话题和我的链接问题似乎暗示......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    相关资源
    最近更新 更多