Özel bir hücreye sahip bir tablo görünümü var. Bir satır seçtikten sonra, bazı şeyler olacak. Satır seçildikten sonra başka şeyler de olacak."Bir İsteğe bağlı değeri çıkarılırken beklenmedik bir şekilde sıfır bulundu" DidDeselectRowAtIndexpath
Her şey yolunda, ancak düzeltemediğim bir hata var. Her istediğim satır ve 'hiçbir şey' olmuyor dokunabilirsiniz ama ardından son satır veya başka bir şekilde ikinci sıradaki dokunun ve eğer etrafta bu hatayı alıyorum:
unexpectedly found nil while unwrapping an Optional value
LLDB bana gösterdi nerede hata oluştu ve bu kod satırında olmalıdır: Bu sorunu çözmek için nasıl bilir herkes
let cell = tableView.cellForRowAtIndexPath(indexPath) as! TaskViewerCell
var mı? Aşağıda her iki işlevin kodunu ekledim.
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
}
Hatanın ne anlama geldiğini biliyorsanız, '!' Kullanmayı durdurun (ve gerçek türün ne olduğunu görmek için hata ayıklayıcısını kullanın) –
Aşağıdaki yanıtlar açıklamanızı sağlar. Potansiyel 'nil' dönüşünü cellForRowAtIndexPath'den ele alıyor, ancak tablo hücrelerindeki durumu izlediğiniz için daha büyük bir sorununuz var. Veri modelinizde durumu izlemeniz gerekir. Hücreler ekranı kapatabilir ve daha sonra 'nil' hücresi – Paulw11
ile karşılaştığınız zaman durum bilgisi kaybolacaktır. (Ne yapar?) Önemli bir hatanın olası kopyası: Beklenmedik şekilde sıfır bulundu, isteğe bağlı bir değer "ortalama?" (Http: //stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – jtbandes