2014-09-10 25 views
3

我今天遷移到Xcode的6轉基因種子,現在我得到以下錯誤:類型「ProfilesTableViewController」並不在Xcode 6轉基因種子遵守協議「UITableViewDataSource」

Type 'ProfilesTableViewController' does not conform to protocol 'UITableViewDataSource'.

我overrided numberOfRowsInSectioncellForRowAtIndexPathnumberOfSectionsInTableView。事實上,一切都運行良好,直到今天。我注意到,當我刪除UITableViewDataSource一切工作正常,並沒有發生錯誤。所以..是否有必要使用'UITableViewDataSource',或者只是覆蓋它的功能?

回答

5

此代碼編譯罰款:

class ProfileTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource { 


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

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

    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("foo", forIndexPath: indexPath) as UITableViewCell 

    return cell 
} 
} 

所以,你絕對可以繼承UITableViewController並定義其UITableViewDataSource協議實現。我現在用的是override關鍵字,也是我不使用自動展開爭論

採取通知 - 即,不喜歡這樣的:

override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { 

這是在以前的Xcode 6個測試版是正確的。

+0

什麼是您的Xcode版本? – 2014-09-12 12:04:57

+0

這是Xcode GM。我的代碼不適合你嗎? – sergio 2014-09-12 21:31:04

+0

事實是,我們都是正確的...因爲你給我作爲一個例子它是從SDK ...但是我需要在ird中的sdk代碼的一個小的修改使用條件assiging ...所以你的代碼工作,我的代碼在我的答案也是... ... :) – 2014-09-15 07:46:52

0

編輯:由於Xcode 6 beta語法:

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){ 

我們必須檢查展開的tableView。所以我用:

 if let tblView = tableView { 
      //code 
     } 

if let theIndexPath = indexPath { 
      //code 
    } 

所以......爲了不改變我的整個項目,我overrided這樣的功能:

override func tableView(tableView: UITableView?, didSelectRowAtIndexPath indexPath: NSIndexPath?){ 

,一切運行良好,除了我的更改ProfileTableViewController不符合UITableViewDataSource,我不得不從類定義中刪除數據源。

+0

我不敢說這是不正確的。請檢查我的答案。 – sergio 2014-09-11 18:18:23

相關問題