獨自看着文檔,試探性的用Swift寫UITableView,遇到個不是很理解的問題。swift
class RootViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{ide
}code
根據以往Obj-C的理解,這句應該沒有問題啊,可是Xcode 6給了一個讓我苦惱好久的提示 : Type 'RootViewController' does not conform to protocol 'UITableViewDataSource'orm
Swift 面世以後的第四天,在網上查到說須要實現 UITableViewDataSource , 以前也想過是否是這個問題,可是方法不能自動聯想出來,我就過濾掉這個問題的可能性了。繼承
而後我就把基本的幾個方法加上了(注意,我遇到的狀況是,下面的方法是不能自動聯想出來的)文檔
func tableView(tableView:UITableView!, numberOfRowsInSection section: Int) -> Int{io
return 10table
}form
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell!{class
let cell = UITableViewCell(style:.Default, reuseIdentifier:"myCell")
cell.textLabel.text = "swift cell \(indexPath.row)"
return cell
}
以前的錯誤消失。到如今仍是很費解。。。。
而後我試了下繼承UITableViewController
class RootViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate{
override func tableView(tableView:UITableView!, numberOfRowsInSection section: Int) -> Int{
return 10
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell!{
let cell = UITableViewCell(style:.Default, reuseIdentifier:"myCell")
cell.textLabel.text = "swift cell \(indexPath.row)"
return cell
}
}
上面兩個方法輸入 "tableview" 時會自動聯想出來,而且有override關鍵字。
目前不知道爲什麼會存在這種現象。
ps: 使用 @IBOutlet 關鍵字可與xib關聯,例:@IBOutlet var tbView:UITableView