Jus*_*tin 23 protocols ios swift
试图在Swift UIViewController子类中符合UITableViewDataSource和UITableViewDelegate.
class GameList: UIViewController {
var aTableView:UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
aTableView.delegate = self
aTableView.dataSource = self
self.view.addSubview(aTableView)
//errors on both lines for not conforming
}
}
Run Code Online (Sandbox Code Playgroud)
文档说你应该遵循这class
条线,:
但这通常是超类的去处.另一个:
不起作用.在超类之后使用逗号分隔列表也不起作用
答案如下. class GameList: UIViewController, UITableViewDataSource, UITableViewDelegate {
还必须采用每个协议的所有必需方法,这是我最初没有做到的.
Fir*_*iro 32
你用逗号:
class GameList: UIViewController, UITableViewDelegate, UITableViewDataSource {
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是要意识到超类必须是逗号分隔列表中的第一项.
如果您未采用协议的所有必需方法,则会出现编译器错误.您必须获得所有必需的方法!
Ber*_*ang 13
随着XCode6-Beta7的发布,
我注意到UITableViewDataSource的协议方法改变了一点点,听起来同样符合协议错误,这在beta6中运行良好.
这些是根据UITableViewDataSource协议实现的必需方法:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // insert code}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // insert code
}
Run Code Online (Sandbox Code Playgroud)
您可能希望重新检查差异或重新实现您认为刚刚实现的委托方法.
小智 7
您必须在此处实现两个require方法:
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
return 10
}
func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
cell.text = "Row #\(indexPath.row)"
cell.detailTextLabel.text = "Subtitle #\(indexPath.row)"
return cell
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30154 次 |
最近记录: |