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 :-)
Sind alle Zellen zu jeder Zeit sichtbar? – Wez
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 :-) –