Jon*_*ant 4 uitableview ios swift
我已经检查了其他一些问题,其答案似乎是在数据源方法的声明中.但是,我找不到我的方法声明有什么问题.
这是我的班级:
import UIKit
class GuestControl: UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var leftTable: UITableView!
@IBOutlet weak var rightTable: UITableView!
var userList = [User]() //even numbers including 0 go on the left table, odd numbers go on the
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//Here is where we link to that user's contact page
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: GuestTableViewCell = tableView.dequeueReusableCellWithIdentifier("guestCell") as! GuestTableViewCell
if tableView == leftTable {
cell.configureCellWithUser(userList[indexPath.row+indexPath.row])
} else {
cell.configureCellWithUser(userList[indexPath.row+indexPath.row+1])
}
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let num = Double(userList.count) / 2.0
if tableView == leftTable {
return Int(round(num)) //round up
} else {
return Int(num) //round down
}
}
Run Code Online (Sandbox Code Playgroud)
}
这是确切的错误:
Protocol requires function 'tableView(_:numberOfRowsInSection:)' with type '(UITableView, numberOfRowsInSection: Int) -> Int'
Candidate is not '@objc', but protocol requires it
Protocol requires function 'tableView(_:cellForRowAtIndexPath:)' with type '(UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell'
Candidate is not '@objc', but protocol requires it
Run Code Online (Sandbox Code Playgroud)
怎么了?
如错误消息所示,协议要求表视图数据源方法是"Objective-C compatible".这可以通过以下方式实现
使您的类继承自NSObject(或任何子NSObject类)
class GuestControl: NSObject, ...
Run Code Online (Sandbox Code Playgroud)或者将@objc属性添加到类定义中
@objc class GuestControl: ...
Run Code Online (Sandbox Code Playgroud)或者将@objc属性添加到数据源方法中
@objc func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ...
Run Code Online (Sandbox Code Playgroud)在前两种情况下,该类的所有方法都与Objective-C兼容.
如果(表)视图控制器用作表视图数据源,那么这将自动满足,因为UI(Table)ViewController继承自NSObject.