【问题标题】:Swift about view does not confirm to protocol "UITableViewDataSource"关于视图的 Swift 未确认协议“UITableViewDataSource”
【发布时间】:2016-10-22 05:54:52
【问题描述】:
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”的错误未确认,我试图找到解决方案,但没有适合我的,有人可以帮助我吗?谢谢

【问题讨论】:

    标签: ios swift swift2


    【解决方案1】:

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

    我相信是:

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

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

    【讨论】:

    • 等等,override 关键字不是必须的吗?
    • @AlexanderMomchliov 如果方法在父类中定义,则只需要overrideUIViewController 没有定义任何表格视图方法。
    • 哦,对了,你不要使用override 来实现协议方法
    • @AlexanderMomchliov 如果您要覆盖父类的实现(此处不是这种情况),则可以这样做。
    【解决方案2】:

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

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
    
        // Configure the cell...
    
        return cell
    }
    
    
     func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-14
      相关资源
      最近更新 更多