Redundant conformance of TableView to protocol UITableViewDataSource with Xib Files

后端 未结 2 1796
时光取名叫无心
时光取名叫无心 2020-12-18 05:34

I have a parent view UIViewController (on storyboard), a TableViewController with .xib and TableViewCell with .xib. I am trying to connect DataSource to the TableView howev

相关标签:
2条回答
  • 2020-12-18 05:41

    When a class inherits from UITableViewController, it by default conforms to UITableViewDataSource & UITableViewDelegate and you need not explicitly specify it.

    You need to conform to UITableViewDataSource and UITableViewDelegate only when you embed a UITableView in a UIViewController.

    0 讨论(0)
  • 2020-12-18 06:03

    There are at least 2 conformations in your class. You need to extend only once.

    First Scenario:

    You conform in the class description AND in the extension.

    class MyViewController: MyDelegate{
        //class functions here
    }
    
    extension MyViewController: MyDelegate{
    func1()
    }
    

    Remove "My Delegate" in the class description.

    class MyViewController{
        //class functions here
    }
    
    extension MyViewController: MyDelegate{
    func1()
    }
    

    Second Scenario:

    You conform in two extensions.

    extension MyViewController: MyDelegate{
    func1()
    }
    
    extension MyViewController: MyDelegate{
    func2()
    }
    

    Merge them into one extension like:

    extension MyViewController: MyDelegate{
    func1()
    func2()
    }
    
    0 讨论(0)
提交回复
热议问题