2016-04-13 20 views
1

Ich erhalte einen Fehler: fatal error: unexpectedly found nil while unwrapping an Optional value, wenn ich meinen Code auf meinem Gerät teste.Fehler beim Aktualisieren auf eine TableView-Zelle

Ich implementiere einen Code zu einem TableViewController, wobei bestimmte TableView-Zellen bei Berührung markiert/gedrückt werden.

Ich habe das Problem auf 'Zeile 6' isoliert, die die letzte Zeile meiner TableView ist. Wenn ich den Code für diese Zeile auskommentiere, läuft der Code perfekt.

ich die Anzahl der Zeilen programmatisch festgelegt haben (und in der Storyboard) bis 7.

Ich sehe einfach nicht, warum der Fehler auftritt. Offensichtlich ist es nicht möglich, die letzte Zelle auszupacken, aber ich verstehe nicht warum.

Hier ist mein Code:

Hinweis: Zeilen 0 und 1 (die ersten beiden Zeilen) nicht anklickbare und somit sind sie nicht in meinem Code verwiesen wird.

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return 7 
} 

//Make TableView Cells Highlight On Touch Respectively. 
override func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) { 

    let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 0)) 
    cell!.contentView.backgroundColor = UIColor(red: 170/255, green: 139/255, blue: 255/255, alpha: 1.0) 


    let cell1 = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 3, inSection: 0)) 
    cell1!.contentView.backgroundColor = UIColor(red: 138/255, green: 231/255, blue: 36/255, alpha: 1.0) 

    let cell3 = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 4, inSection: 0)) 
    cell3!.contentView.backgroundColor = UIColor(red: 135/255, green: 190/255, blue: 255/255, alpha: 1.0) 

    let cell4 = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 5, inSection: 0)) 
    cell4!.contentView.backgroundColor = UIColor(red: 250/255, green: 236/255, blue: 67/255, alpha: 1.0) 

    let cell5 = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 6, inSection: 0)) 
    cell5!.contentView.backgroundColor = UIColor(red: 182/255, green: 181/255, blue: 182/255, alpha: 1.0) 


} 

override func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) { 

    let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 0)) 
    cell!.contentView.backgroundColor = .clearColor() 

    let cell1 = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 3, inSection: 0)) 
    cell1!.contentView.backgroundColor = .clearColor() 


    let cell3 = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 4, inSection: 0)) 
    cell3!.contentView.backgroundColor = .clearColor() 

    let cell4 = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 5, inSection: 0)) 
    cell4!.contentView.backgroundColor = .clearColor() 

    let cell5 = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 6, inSection: 0)) 
    cell5!.contentView.backgroundColor = .clearColor() 

} 

Vielen Dank für Ihre Zeit im Voraus :-)

+0

Sind alle Zellen zu jeder Zeit sichtbar? – Wez

+0

Ein Teil der letzten Zelle ist im StoryBoard ausgeblendet (Navigationsleiste oben). Aber es sollte immer noch "sichtbar" sein, wenn ich das Projekt leite. Die Antwort unten löste mein Problem - @ Andrew McKinley - Kredit für ihn :-) –

Antwort

1

Vermeiden Sie das erzwungene Auspacken zu fast allen Kosten!

if let cell5 = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 6, inSection: 0)){ 
    cell5.contentView.backgroundColor = UIColor(red: 182/255, green: 181/255, blue: 182/255, alpha: 1.0) 
} 
+0

stimmte damit überein, dass er Ratschläge auspacken musste. Hat mir viele Probleme bereitet! Wird von nun an Ihrem Rat folgen. Sehr geschätzt. –

2

Laut Apple-Dokumentation für cellForRowAtIndexPath

Rückgabewert Ein Objekt einer Zelle der Tabelle darstellt, oder null, wenn die Zelle nicht sichtbar oder indexPath liegt außerhalb des Bereichs.

Sie machen Instanzvariablen in der Methode, um auf die Zellen zu verweisen und sie in der nächsten Zeile auszupacken. Die letzte Zelle, die Sie versuchen auszupacken, ist wahrscheinlich nicht sichtbar und die Methode gibt nil zurück, weil die Zelle nicht existiert/die Zelle für diese Zeile wurde nicht entfernt.

Sie möchten wahrscheinlich entweder mit Zellen gegen Zellen schützen, wenn Sie let, wenn Sie wirklich wollen, dass alle Zellen ihre Farbe ändern, oder mit einer switch-Anweisung, wenn Sie nur die Farbe einer Zelle ändern wollen.

+0

Ordentlich vermerkt Sir, mit Dankbarkeit. –

Verwandte Themen