2015-08-02 5 views
15

mi sono imbattuto nel seguente errore in questo codice:UITableViewCell Errore Xcode 7/Swift 2

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell 

ERROR: '? UITableViewCell' Downcast da a 'UITableViewCell' solo riattiva gli optionals; volevi usare '!'?

Qualsiasi idea?

+0

Modifica 'UITableViewCell?' A 'UITableViewCell!' – Kendel

risposta

30

In Swift2.0 metodo dequeueReusableCellWithIdentifier è dichiarare come:

@available(iOS 6.0, *) 
func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> UITableViewCell 

Non si dovrebbe lanciare UITableViewCell-UITableViewCell?. Vedi il codice qui sotto.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 

    // Configure the cell... 

    return cell 
} 

Spero che questo aiuti!

+0

Questa è la risposta corretta. +1 – dhruvvyas90

0

Utilizzare questo invece

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") 
-2
var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as UITableViewCell! 
+2

Benvenuti in SO! Fornisci sempre spiegazioni alle tue risposte. Le risposte al solo codice sono raramente utili. Prima di tutto tutti quelli che leggono la tua risposta devono capire cosa vuoi dire (qui è solo una differenza di un personaggio), e quindi dobbiamo tutti indovinare perché la proponi come una soluzione. Chiedi sempre a te stesso, perché la tua risposta è migliore rispetto alle altre risposte già esistenti? Se non riesci a spiegarlo, non pubblicare. – cfi

3

Come di Xcode 7, dequeueReusableCellWithIdentifier restituirà sempre un optional UITableViewCell non.

Non c'è nemmeno bisogno di specificare il tipo, può essere scritto succintamente:

let cell = tableView.dequeueReusableCellWithIdentifier("Cell") 

o se si dispone di una sottoclasse personalizzata di UITableViewCell

guard let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? SomeOtherCell else { fatalError("unexpected cell dequeued from tableView") } 
-1

se si desidera utilizzare identificatore si dovrebbe utilizzare gli stessi metodi:

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


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("WHAT-EVER-YOU-WANT-TO-CALL-IT", forIndexPath: indexPath) 
    let label = cell.viewWithTag(1000) as! UILabel 
} 
    return cell 
0
CMessageCell=self.MessageTable.dequeueReusableCellWithIdentifier("CustomMessageCell") as! CustomMessageCell