2017-05-26 59 views
0

我已经开始了我的第一个应用程序,并且我希望应用程序具有作为待办事项列表的视图,但是,我得到类型'ThirdViewController'不符合协议UITableViewDataSource作为我的代码中的错误。我已经看了一个类似的线程,但没有找到解决办法有类型'ThirdViewController'不符合协议UITableViewDataSource

import UIKit 

class ThirdViewController: UIViewController, UITableViewDataSource, UITableViewDelegate 
{ 
    var list = ["Math Homework", "English Project", "Bio Quiz"] 

    @IBOutlet weak var myTableView: UITableView! 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() 
    { 
     super.didReceiveMemoryWarning() 
    } 


    public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return list.count 
    } 

    public func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell") 
     cell.textLabel?.text = list[indexPath.row] 

     return cell 
    } 
} 

回答

1

由于苹果开发者文档中指定,则需要以下方法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

你,而不是使用了这些:

public func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell 
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

从两种方法删除public,它应该工作。另外,在tableView:之前加_

参考:

https://developer.apple.com/reference/uikit/uitableviewdatasource

编辑: 我描述的是斯威夫特3.如果您使用的是雨燕2.3的方法,这些应该是你的方法:

func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

几乎与第一次使用的相同,但没有public

+0

清除了错误,但现在我得到了“使用未声明的类型'IndexPath'。我该如何解决这个问题? –

+0

您在哪一行收到错误?另外,'indexPath'应该以小写字母'i '。 – vitormm

+0

func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell - 在你的两个建议编辑中我得到了同样的错误 –

0

实施UITableViewDataSource和的UITableViewDelegate所需的协议的方法。

你缺少;

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
+0

试过了,仍然显示相同的错误。 –

相关问题