xcode - 无法将 UITableViewDataSouce 和 UITableViewDelegate 设置为 UIViewController

标签 xcode uitableview uiviewcontroller swift xcode6

当我尝试将委托(delegate)和数据源设置为 UIViewController 时,所有在 UITableViewController 中正常运行的函数都被检测为错误。

func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 10
}

错误是

FirstViewController.swift:11:1: Type 'FirstViewController' does not conform to protocol 'UITableViewDataSource':

UIKit.UITableViewDataSource:2:48: Protocol requires function      'tableView(_:numberOfRowsInSection:)' with type '(UITableView, numberOfRowsInSection: Int) -> Int'
FirstViewController.swift:29:10: Candidate has non-matching type '(UITableView!, numberOfRowsInSection: Int) -> Int'
UIKit.UITableViewDataSource:3:48: Protocol requires function 'tableView(_:cellForRowAtIndexPath:)' with type '(UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell'
FirstViewController.swift:29:10: Candidate has non-matching type '(UITableView!, numberOfRowsInSection: Int) -> Int'

这是 Xcode 6(不是测试版)。 UIViewController 中是否不再支持 TableView 委托(delegate)?

(相同的代码在 Xcode 6 Beta 5 中有效,但在 Xcode 6 GM 中无效。)

最佳答案

您正在使用 implicitly unwrapped optionals (UITableView!)。但是这段代码不再使用可选值。

方法签名现在看起来像这样:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 0;
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 0;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return UITableViewCell()
}

关于xcode - 无法将 UITableViewDataSouce 和 UITableViewDelegate 设置为 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25861915/

相关文章:

ios - 在不覆盖 iPhone 上的当前 View 的情况下呈现模态视图 Controller

iphone - 如何在Xcode 4.2中开发NavigationController而不使用storyboard和tableView?

xcode - 为什么 Xcode 在文件夹引用中找不到标题?

ios - 在 viewDidLoad IOS 10 中获取内部 View 大小

快速添加可点击的表格 View

ios - 在一个 UIViewcontroller 中使用两个 View

xcode - CABasicAnimation 不适用于 UIImageView

iphone - 如何修改UIDatePicker的内容?

ios - 重新加载节中的标题,而不用快速重新加载节行

objective-c - 我可以在 UITableView 的 allowsMultipleSelectionDuringEditing 设置为 YES 时使用自定义复选标记吗?