2014-10-01 11 views
6

Próbuję dodać wiele podklas do UITableView. Problemem jest to, że utrzymanie daje mi następujący błąd:Swift wielu podklas komórek w UITableViewCOntroller

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 
} 

Odpowiedz

-3

Nie wolno powrócić nil dla cellForRowAtIndexPath

Można użyć na koniec:

let cell:UITableViewCell! 
return cell 

zamiast

return nil 

To powinno być (jeśli masz tylko 2 sekcje) nigdy nie zostanie wykonane.

Edit:

Można także użyć zamiast "} else if indexPath.section == 1 {" tylko} else {- powrót komórkę. Właśnie pokazałem, na czym polega problem. Lub użyj przełącznika/obudowy i zwróć pustą komórkę domyślnie.

+0

Rozwiązanie to generuje ostrzeżenie z Xcode 6: 'Niezmienne jest wartość domyślna zainicjowany i nigdy nie może być changed'. Co więcej, tworzenie nieopakowanego opcjonalnie opcji inicjalizowanej zerą i która, mam nadzieję, nigdy nie jest wywoływana, aby wyciszyć komunikat o błędzie dla metody, która wymaga zwrócenia nie-opcjonalnej 'UITableViewCell', wygląda na zły projekt kodu. –

+0

Może również użyć zamiast "} else if indexPath.section == 1 {" only} else {- aby zwrócić komórkę. Właśnie pokazałem, na czym polega problem. Lub użyj przełącznika/obudowy i zwróć pustą komórkę domyślnie. – derdida

6

tableView:cellForRowAtIndexPath: musi zwrócić UITableViewCell i nie może zwrócić nil. Musisz usunąć return nil. Ale to nie wystarczy. Twoje oświadczenie if else również musi być kompletne. Oznacza to, że należy podać każdą możliwą wartość section lub przynajmniej wysłać do upadku.

Twój if else stwierdzenie powinno wyglądać następująco:

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

Albo tak:

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

Jednak po if else oświadczenie nie jest kompletna:

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

W tym przypadku , tableView:cellForRowAtIndexPath: nie będzie wiedział, co zwrócić, jeśli o f Weryfikacja tych warunków. Tak więc, jeśli nie spróbujesz, Xcode (czyli mądry) będzie również wyświetlać komunikat o błędzie:

Missing return in a function expected to return 'UITableViewCell'

Dlatego poniższy kod powinien działać:

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:

Jeśli używasz również instrukcji switch w metodzie tableView:cellForRowAtIndexPath: w celu ustawienia komórek, może Ci pomóc this answer to a similar question.

8

Nie można wrócić do zera. Zamiast tego domyślnie zwracana jest pusta komórka.

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 

Upewnij się, że zarejestrowałeś stalówkę

self.tableView.registerNib(UINib(nibName: "SwitchViewCell", bundle: nil), forCellReuseIdentifier: "SwitchViewCell") 
Powiązane problemy