2016-10-22 4 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」

これは私のプログラムのコードでは、私はto-doリストアプリを作成しようとしていますが、エラーは、プロトコルに確認しません"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メソッドが親クラスで定義されている場合は、 'override'が必要です。 'UIViewController'はテーブルビューメソッドを定義しません。 – rmaddy

+0

ああ、プロトコルメソッドの実装には 'override'を使用しないでください – Alexander

0

はい、あなたが混合されているSWIFT 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 
    } 
} 

、あなたのメソッドの上

関連する問題