【发布时间】:2014-08-30 11:19:22
【问题描述】:
开始快速练习。在 singleViewController 中,我试图创建一个 UITableView。在情节提要中,我设置了数据源和委托。在这里我收到错误 * 'ViewController' does not conform to protocol 'UITableViewDataSource' *
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
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.
}
}
func numberOfSectionsInTableView(tableView: UITableView!) -> Int
{
return 20
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel.text="row#\(indexPath.row)"
cell.detailTextLabel.text="subtitle#\(indexPath.row)"
return cell
}
【问题讨论】:
-
您的函数
numberOfSectionsInTableView和cellForRowAtIndexPath需要在类中。将它们向上移动,就在didReceiveMemoryWarning的定义下方。 -
错误消息明确指出要实现 UITableViewDataSource 的“必需”委托,即 numberOfRowsInSection 和 cellForRowAtIndexPath 方法。
-
编译器抛出该错误的第一个原因是它正在寻找它在 UITableViewDatasource 中找到的所需方法,而您缺少 numberOfRowsInSection 方法。
-
您如何真正知道需要实施哪些委托?我控制点击 UITableViewDataSource 并列出了几个方法
标签: ios uitableview swift