2016-10-22 23 views
0
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”

这是我的程序的代码,我试图做一个待办事项列表应用程序,但错误没有确认到协议“UITableViewDataSource”总是出现,我试图找到解决方案,但没有适合我的,任何人都可以帮助我?谢谢

回答

1

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

我相信它是:

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

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

+0

等待,这里不是必须的'override'关键字吗? – Alexander

+1

@AlexanderMomchliov如果方法在父类中定义,则只需要“覆盖”。 'UIViewController'没有定义任何表视图方法。 – rmaddy

+0

噢,对于协议方法实现,你不使用'override' – Alexander

0

是的,你是混合迅速2种,迅速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 
    } 
} 

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