2014-10-01 67 views
6

我想將多個子類添加到UITableView中。問題是,它不斷給我下面的錯誤:UITableViewCOntroller中的swift多個單元子類

Type UITableVieCell does not conform to protocol NilLiteralConvertible 

的cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if indexPath.section == 0 { 

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

     cell.textLabel?.text = self.section1[indexPath.row] 
     cell.accessoryType = .DisclosureIndicator 

     return cell 

    } else if indexPath.section == 1 { 

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

     cell.cellLabel?.text = self.section2[indexPath.row] 

     return cell 
    } 

    return nil 
} 

回答

-3

你不允許爲的cellForRowAtIndexPath返回nil

你可以在最後使用:

let cell:UITableViewCell! 
return cell 

而不是

return nil 

這應該是(如果你只有2個部分)永遠不會被執行。

編輯:

你也可以使用,而不是 「}否則,如果indexPath.section == 1 {」 只}其他{ - 返回一個單元格。我剛剛出現了什麼問題。或者使用開關/大小寫,並在默認情況下返回空單元格。

+0

該解決方案生成和Xcode 6警告:'不可變值是默認初始化,無法再changed'。此外,創建一個隱式解包的可選初始化爲nil,希望永遠不會調用,以便爲需要返回非可選'UITableViewCell'的方法提供錯誤消息,這看起來就像錯誤的代碼設計。 – 2014-10-01 14:47:43

+0

他也可以使用,而不是「} else if indexPath.section == 1 {」only} else { - 返回一個單元格。我剛剛出現了什麼問題。或者使用開關/大小寫,並在默認情況下返回空單元格。 – derdida 2014-10-01 14:50:37

6

tableView:cellForRowAtIndexPath:必須返回UITableViewCell並且不能返回nil。所以你將不得不刪除return nil。但這還不夠。您的if else聲明也必須完整。這意味着必須提供每種可能的section價值,或者至少發送一次跌價。

if else說法應該是這樣的:

if indexPath.section == 0 { 
    /* ... */ 
} else if indexPath.section == 1 { 
    /* ... */ 
} else { 
    /* ... */ 
} 

或者這樣:

if indexPath.section == 0 { 
    /* ... */ 
} else { 
    /* ... */ 
} 

但是,下面的if else說法是不完整的:

if indexPath.section == 0 { 
    /* ... */ 
} else if indexPath.section == 1 { 
    /* ... */ 
} 

在這種情況下, ,tableView:cellForRowAtIndexPath:將不知道如果有任何o返回f驗證這些條件。因此,如果你想這樣,Xcode中(也就是智能)也將顯示一條錯誤消息:

Missing return in a function expected to return 'UITableViewCell'

因此,下面的代碼應工作:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if indexPath.section == 0 { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

     cell.textLabel?.text = self.section1[indexPath.row] 
     cell.accessoryType = .DisclosureIndicator 

     return cell 
    } else { 
     let cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell 

     cell.cellLabel?.text = self.section2[indexPath.row] 

     return cell 
    } 
} 

PS:

如果您還在tableView:cellForRowAtIndexPath:方法中使用switch聲明來設置單元格,this answer to a similar question可能會對您有所幫助。

8

你不能返回零。相反默認情況下返回空單元格。

var cell :UITableViewCell! 

    switch (indexPath.row) { 
    case 0: 
     cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
     cell.textLabel?.text = self.section1[indexPath.row] 
     cell.accessoryType = .DisclosureIndicator 
     break; 

    case 1: 
     cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell 
     (cell as SwitchViewCell).cellLabel?.text = self.section2[indexPath.row] 
     break; 

    default: 
     break; 
    } 

    return cell 

確保您已註冊的筆尖

self.tableView.registerNib(UINib(nibName: "SwitchViewCell", bundle: nil), forCellReuseIdentifier: "SwitchViewCell") 
相關問題