【问题标题】:Why the "Use of undeclared type UITableView" happened in Swift?为什么在 Swift 中会出现“使用未声明类型的 UITableView”?
【发布时间】:2015-01-19 03:36:37
【问题描述】:

我是Swift 的新手。我创建了第二个swift file 调用DeviceInfo.swift 并在Main.storyboard 中添加了一个UIViewcontroller。

我还在UIViewcontroller(DeviceInfo) 中添加了UITableView。

但在我通过command + left 将UITableView 连接到DeviceInfo.swift 后,出现错误Use of undeclared type 'UITableView'。错误显示如下。

问题

1.为什么Use of undeclared type 'UITableView' 发生了?如何解决?

2。显示我将dataSource 和delegate 连接到UIViewcontroller(DeviceInfo)?

提前致谢。

【问题讨论】:

    标签: ios uitableview swift


    【解决方案1】:

    1.import UIKit

    2.连接delegate和dataSource, 单击该点并拖动到您的 tableView。

    【讨论】:

    • 您的意思是,将UITableView 的delegate 和dataSource 连接到View 而不是UIViewController?为什么?
    • 我犯了一个错误。是的,应该是 UIViewController。因为你的 UIViewController 需要知道如何展示数据,而 tableView 需要让 UIViewController 去做一些 tableView 自己做不到的事情。比如didSelectedRowAtIndex或者其他。
    【解决方案2】:

    您需要在文件顶部添加import UIKit,在您已经获得import Foundation 的区域中。如果没有导入 UIKit,您的对象将不知道 UITableView 是什么。

    【讨论】:

    • 关于问题 2,datasource 和 delegate 应该连接什么? UIViewController 或`查看`?
    • @Martin UIViewController
    【解决方案3】:

    UITableView 在 UIKit 框架中,你没有导入它。所以import UIKit.

    在你的viewDidLod:

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    

    在您的 .h 文件中,您可以:

    @interface YourClassName : UIViewController<UITableViewDataSource,UITableViewDelegate>
    

    并确保您使用IBOutlet 连接了tableView。

    【讨论】:

    • 关于问题 2,datasource 和 delegate 应该连接什么? UIViewController 或`查看`?
    猜你喜欢
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多