当我尝试将委托(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/