2016-09-15 16 views
7

我最近使用Xcode 8.0將項目轉換爲Swift 3,並且我在函數上遇到了一個錯誤,我不太明白。在這些線路上:Swift 3.0 UITableViewDelege Objective-c方法與需求選擇器不匹配

extension HomeTableViewController : UITableViewDelegate { 

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 

    } 
} 

要解決此錯誤時,Xcode告訴我,就在方法前加@objc(tableView:commitEditingStyle:forRowAtIndexPath:)

Xcode error

好了,它的工作原理,但我不知道爲什麼它只是需要這種方法。

Xcode不需要在我的tableView:heighForHeaderInSection前添加@objc的東西,但我沒有看到在此方法和tableView:commitEditingStyle:forRowAtIndexPath:之間UITableViewDelegate的任何差異。

那麼,知道爲什麼這是tableView:commitEditingStyle:forRowAtIndexPath方法的強制性?

在此先感謝!

+0

我有同樣的問題@objc(tableView:heightForRowAtIndexPath :)方法。不知道爲什麼 – Maiaux

回答

10

您在擴展中採用了錯誤的協議。 tableView:commitEditingStyle:forRowAtIndexPath:方法是UITableViewDataSource協議的一部分。更改您的分機採用UITableViewDataSource協議而不是UITableViewDelegate協議,錯誤消失。

至少這對我來說當我得到這個錯誤NSCollectionViewDelegate/DataSource擴展。

+0

哦,非常感謝,我沒有注意到! PEBCAK:/很奇怪,它沒有用Swift 2.2發出警告 – Toldy