关于视图的Swift没有确认协议“UITableViewDataSource”

时间:2016-10-22 05:54:53

标签: ios swift swift2

class showPageViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


    var records : [Record] = []
@IBOutlet weak var tableView: 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 numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

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

    return UITableViewCell()


}

func tableView(_ tableVoew: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    if editingStyle == .delete{
        let record = records[indexPath.row]
        context.delete(record)
        (UIApplication.shared.delegate as! AppDelegate).saveContext()
        do{
            records = try context.fetch(Record.fetchRequest())
        } catch{
            print("Failed")
        }
    }


}

override func viewWillAppear(_ animated: Bool) {
    getData()
    tableView.reloadData()
}

func getData(){
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    do{
        records = try context.fetch(Record.fetchRequest())
    } catch{
        print("123")
    }
}

}

这是我的程序的代码,我正在尝试制作一个待办事项列表应用程序,但错误并未确认协议“UITableViewDataSource”总是出现,我试图找到解决方案,但没有什么适合我,有人可以帮帮我吗?感谢

2 个答案:

答案 0 :(得分:1)

您的代码中混合了Swift 2和Swift 3方法。您的cellForRow...方法正在使用较新的Swift 3签名。您需要使用较旧的Swift 2兼容版本。

我相信它是:

func tableview(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}

检查所有其他表视图数据源和委托方法,并确保它们都使用Swift 2兼容签名,而不是较新的Swift 3签名。

答案 1 :(得分:0)

是的,你正在混合swift 2和swift 3.你的方法应该是这样的:

exit 0

你在这些方法中犯了几个拼写错误,请用你的方法检查上述方法