TableView 与 Xib 文件的协议 UITableViewDataSource 的冗余一致性

Redundant conformance of TableView to protocol UITableViewDataSource with Xib Files

我有一个父视图 UIViewController(在情节提要上)、一个带有 .xib 的 TableViewController 和带有 .xib 的 TableViewCell。我正在尝试将 DataSource 连接到 TableView,但它给我一个错误:

Redundant conformance of 'TableView1' to protocol 'UITableViewDataSource'

'TableView1' inherits conformance to protocol 'UITableViewDataSource' from superclass here.

不在 class 附近添加数据源并将其作为 class TableView1: UITableViewController {.. 尝试,它不会给我任何错误,并且在模拟器中,我可以看到 table 视图错觉,当我向下滚动。

但是,当我尝试添加数据源时,出现了这些错误。

我在设置它时遵循的路径...:[=​​17=]

  1. Ctrl + 从 xib 拖动到 TableView1 并将其连接为 Globals

  2. 在 xib 文件中,我连接了 DataSource & Delegate

  1. 最后,我的 TableView1:

class TableView1: UITableViewController, UITableViewDataSource { 这里出错..

@IBOutlet var GlobalsTableView: UITableView!

var results: [AnyObject]? = []

override func viewDidLoad() {
    super.viewDidLoad()
        print("A")
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return self.results?.count ?? 0
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! DogTableViewCell

    return cell
 }
}

请注意,在 TableView1.xib 中,我不能 select TableView1 作为自定义 Class -> Class(但我不认为这是必要的)。

当class 继承自UITableViewController 时,它默认符合UITableViewDataSource & UITableViewDelegate,您无需明确指定。

只有在UIViewController中嵌入UITableView时才需要符合UITableViewDataSourceUITableViewDelegate

你的 class 中至少有 2 个构象。您只需延长一次。

第一个场景:

您符合 class 描述和扩展。

class MyViewController: MyDelegate{
    //class functions here
}

extension MyViewController: MyDelegate{
func1()
}

删除 class 描述中的 "My Delegate"。

class MyViewController{
    //class functions here
}

extension MyViewController: MyDelegate{
func1()
}

第二种情况:

您符合两个扩展。

extension MyViewController: MyDelegate{
func1()
}

extension MyViewController: MyDelegate{
func2()
}

将它们合并为一个扩展,如:

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