【发布时间】:2014-10-20 19:04:47
【问题描述】:
我在 Xcode 6 (Swift) 中编写了这个,但它说“类型 'FirstViewController' 不符合协议 'UITableViewDataSource'”并且不会让我构建程序。请帮忙?
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//UIViewTableDataSource
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return taskMGR.tasks.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->
UITableViewCell!{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:
"test")
cell.textLabel?.text = taskMGR.tasks[indexPath.row].name
cell.detailTextLabel?.text = taskMGR.tasks[indexPath.row].desc
return cell
}
}
【问题讨论】:
-
你在这两个dataSource方法前面缺少
override... -
另外:与其遵循
UIViewController、UITableViewDelegate和UITableViewDataSource,不如让该类成为UITableViewController的子类... -
在 viewDidLoad 中以编程方式设置数据源和委托
标签: ios