独自看着文档,试探性的用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