2014-07-18 8 views
12

Abbastanza semplice:Semplice UITableView a Swift - inaspettatamente trovato il codice zero

func numberOfSectionsInTableView(tableView: UITableView?) -> Int { 
    return 1 
} 

func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int { 
    return 5 
} 


func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! { 
    let cell: BookTableViewCell = BookTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "BookCell") 
    println("ip: \(indexPath.row)") 
    cell.bookLabel.text = "test" 

    return cell 
} 

Sulla linea cell.bookLabel.text ottengo questo:

fatal error: unexpectedly found nil while unwrapping an Optional value 

Il BookTableViewCell è definita in questo modo:

class BookTableViewCell: UITableViewCell { 

    @IBOutlet var bookLabel: UILabel 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 

E bookLabel è correttamente collegato in una cella Prototype nello Storyboard. Perché ricevo questo errore?

risposta

7

Quando si crea una vista nel codice, le sue proprietà IBOutlet non vengono collegate correttamente. Si desidera che la versione che si torna da dequeueReusableCellWithIdentifier:

let cell = tableView.dequeueReusableCellWithIdentifier("BookCell") as BookTableViewCell 
+12

Ancora non funziona per me. – JohnVanDijk

57

Se stai usando storyboard, assicurarsi che non si dispone di questa linea all'inizio del file:

self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell") 

Sarà sovrascrivi lo storyboard e, di conseguenza, i link di uscita nello storyboard vengono ignorati.

+6

Questa era la risposta giusta per me ... mi stava strappando i capelli. –

+3

grazie! questa è la risposta – Led

+0

Grazie mille. Ero così abituato a scrivere questo quando stavo facendo tutto a livello di programmazione. – Cesare

15

Stavo ricevendo questo errore perché non avevo l'identificatore scritto nello Storyboard della cella personalizzata.

StoryBoard

anche accertarsi che corrisponda si codice:

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

     let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableCell") as CustomTableCell 

     ... 
    } 
6

Forse che la visualizzazione in Main.Storyboard ha perso il suo riferimento nel file di IBOutlet ViewController, basta collegarlo di nuovo.

+0

Questo è semplice ma finalmente funziona per me. –

+0

Nel mio caso ho avuto un riferimento IBOutlet stantio che stava causando questo errore. Grazie per il suggerimento. –

1

Nel mio caso è stato il modo in cui l'opzionale è da scartare:

let cellId:String = "ConverterTableCell" 
let cell: ConverterTableViewCell = (tableView.dequeueReusableCellWithIdentifier(cellId)! as? ConverterTableViewCell)! 
0

Prova a fare questo:

let cell = tableView.dequeueReusableCellWithIdentifier("BookCell", forIndexPath: indexPath) as! BookTableViewCell 

e non dimenticare di impostare l'identificatore di riutilizzo nel vostro storyboard

0

Viene visualizzato questo errore ogni volta che utilizzo riutilizzare Identifer nome diverso da nome classe personalizzato unifid questi nomi risolverlo per me

1

Non dimenticare di registrare pennino (testato con Swift3), e. g. all'interno Override func viewDidLoad():

self.tableView.register(UINib(nibName: "BookTableViewCell", bundle: nil), forCellReuseIdentifier: "BookCell") 
0

ho incontrato questo errore, se metto UITapGestureRecognizer in un costume UITableViewCell sullo storyboard (versione Xcode è 8.3)..

Problemi correlati