2016-11-28 72 views
0

我不理解为什么我的应用程序不编译。这是目前的输出:与“类型'ViewController'相关的编译和生成错误不符合协议'UITableViewDataSource'”

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    var IndexArray = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"] 
    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 IndexArray.count 
    } 

    func tableView(tableView: UITableView, tiltleForHeaderInSection section: Int) -> String? { 
     return IndexArray[section] 
    } 

    func sectionIndexTitlesfortableView (tableView: UITableView) -> [String]? { 
     return IndexArray 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 3 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath as IndexPath) as! TableCell 

     cell.imgPhoto.image = UIImage(named: "charity") 
     cell.lblUserName.text! = "User Name" 

     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    } 
} 
+0

您所有的实现代码如下方法有拼写错误/ miscapitalized单词或正在使用的是快速的3种不同的SWIFT 2个方法签名。使用Xcode的自动完成来获得正确的签名。 – dan

+0

你在谈什么Swift版本?该代码是一个Swift 2/3混合调整器。 – vadian

+0

我相信我正在使用Swift版本3.我不确定如何确定Swift版本。 –

回答

0

您缺少指定在您的类派生的协议中声明的几个方法。

func tableView(UITableView,cellForRowAt:IndexPath) 必需。请求数据源为单元插入表视图的特定位置。

func tableView(UITableView,numberOfRowsInSection:Int) 必需。通知数据源返回表视图给定部分中的行数。

至少上面的两个方法必须在你的类中声明,否则你会得到错误。

这些只是所需的方法,但为了以正确的方式运行,您需要定义其他方法。查看UITableViewDataSource协议苹果文档

+0

在我观看的视频中,上面的代码全部包含在内,他的应用程序能够成功启动。应该更改代码以便应用程序成功编译? –

+0

也许这个视频是为较老的iOS版本制作的......很难说。实施标记为您决定添加的协议所需的功能,并且错误应该消失。 – Sergiob

0

在斯威夫特3所有方法签名已更改为:

func numberOfSections(in tableView: UITableView) -> Int { } 

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { } 

func sectionIndexTitles(for tableView: UITableView) -> [String]? { } 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { } 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { } 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {} 
相关问题