I have a table view with a custom cell. Once you select a row, some things will happen. Once the row gets deselected, some other things will happen.
This all works fine, but there is one bug I can't fix. I can tap on every row I want and 'nothing' happens, but if I tap on the second row and then the last row or the other way around I get this error:
unexpectedly found nil while unwrapping an Optional value
The LLDB showed me where the error occurred and it should be in this line of code:
let cell = tableView.cellForRowAtIndexPath(indexPath) as! TaskViewerCell
Is there anybody who knows how to fix this issue? I've added the code of both functions below.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! TaskViewerCell
number = indexPath.row
if cell.submit.hidden != false || next[0..<indexPath.row].contains(false){
cell.submit.setTitle("Not completed", forState: .Normal)
cell.submit.setTitleColor(UIColor.orangeColor(), forState: .Normal)
logbook = "Pending"
}else{
logbook = "completed"
cell.submit.setTitle("Completed", forState: .Normal)
cell.submit.setTitleColor(UIColor.greenColor(), forState: .Normal)
}
if indexPath.row != 0 && next[indexPath.row-1] == true{
cell.nSwitch.enabled = true
cell.nSegment.enabled=true
cell.nButton.enabled = true
cell.submit.enabled = true
print("tapped\(indexPath.row)")
}else{
cell.nSwitch.enabled = false
cell.nSegment.enabled=false
cell.nButton.enabled = false
cell.submit.enabled = false
}
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! TaskViewerCell // This is where is should go wrong!!
switch1[indexPath.row] = cell.nSwitch.on
yesno1[indexPath.row] = cell.nSegment.selectedSegmentIndex
if cell.nField != "" {
field1[indexPath.row] = cell.nField.text!
}else{
field1[indexPath.row] = cell.nField.placeholder!
}
topField[indexPath.row] = cell.topField.text!
topSegment[indexPath.row] = cell.topSegment.selectedSegmentIndex
cell.nSwitch.enabled = false
cell.nSegment.enabled = false
cell.nButton.enabled = false
cell.submit.enabled = false
}