2017-05-25 82 views
1

我創建的應用程序涉及到使用tableview,當選中一個單元格時,數量值是按價格計算的時間,然後從總和中增加或減去該值,取決於細胞正在被選擇或取消選擇的天氣。然而,每次我的應用程序崩潰時,我都會在一個單元格中使用deselecttableview中選擇和取消選擇單元格之間的差異

I am encountering the error "fatal error: unexpectedly found nil while unwrapping an Optional value" I figure there is a discrepancy between the two function but I cannot find it.

任何幫助將不勝感激!

感謝,

繼承人我的代碼:

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    let cell = thistableview.cellForRow(at: indexPath) as! TableViewCellcanteen 
    cell.qtyValueThing.isUserInteractionEnabled = false 

    let somevar = itemsarray[indexPath.row] 
    aselect.append("\(somevar)") 

    let somevar2 = pricearray[indexPath.row] 
    aselect.append("\(somevar2)") 

    let thisvarthe = pricearray2[indexPath.row] 
    value = Int(cell.qtyValueThing.text!)! 

    aselect.append("\(String(describing: value))") 
    amountToSave = Double(Float(thisvarthe) * Float(value)) 
    totalvalue = Double(Double(amountToSave) + Double(totalvalue)) 

    let totalvaluerounded = String(format: "%.2f", totalvalue) 

    Total.title = "$" + "\(totalvaluerounded)" 

} 

public func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 

    let cell = self.thistableview.cellForRow(at: indexPath) as! TableViewCellcanteen 
    cell.qtyValueThing.isUserInteractionEnabled = true 
    let somevar3 = itemsarray[indexPath.row] 

    if aselect.contains(somevar3){ 

     let intToRemove = aselect.index(of: somevar3) 

     aselect.remove(at: intToRemove!) 
    } 

    let somevar5 = pricearray[indexPath.row] 

    if aselect.contains(somevar5){ 
     let intToRemove = aselect.index(of: somevar5) 
     aselect.remove(at: intToRemove!) 
    } 

    let thisvar2 = pricearray2[indexPath.row] 

    //error is occuring below 
    value = Int(cell.qtyValueThing.text!)! 
    //error is fatal error: unexpectedly found nil while unwrapping an Optional value 


    if aselect.contains(String(value)){ 
     let intToRemove = aselect.index(of: String(value)) 
     aselect.remove(at: intToRemove!) 
    } 

    amountToSave = Double(Float(thisvar2) * Float(String(describing: value))!) 
    totalvalue = Double(Float(totalvalue) - Float(amountToSave)) 



    let totalvaluerounded = String(format: "%.2f", totalvalue) 

    Total.title = "$" + "\(totalvaluerounded)" 
} 

回答

1

我想通了,在這裏它去

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    let cell = thistableview.cellForRow(at: indexPath) as! TableViewCellcanteen 
    cell.qtyValueThing.isUserInteractionEnabled = false 


    if cell.qtyValueThing.text! != nil{ 
     if cell.qtyValueThing.text == ""{ 
      cell.qtyValueThing.resignFirstResponder() 
      cell.qtyValueThing.text = "1" 
     } 
    let somevar = itemsarray[indexPath.row] 
    aselect.append("\(somevar)") 

    let somevar2 = pricearray[indexPath.row] 
    aselect.append("\(somevar2)") 

    let thisvarthe = pricearray2[indexPath.row] 

    print(cell.qtyValueThing.text!) 
    value = Double(cell.qtyValueThing.text!)! 

    aselect.append("\(String(describing: value))") 
    amountToSave = Double(Float(thisvarthe) * Float(value)) 
    totalvalue = Double(Double(amountToSave) + Double(totalvalue)) 

    let totalvaluerounded = String(format: "%.2f", totalvalue) 

    Total.title = "$" + "\(totalvaluerounded)" 
    } 
} 

public func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 

    let cell = self.thistableview.cellForRow(at: indexPath) as! TableViewCellcanteen 

    if cell.qtyValueThing.text! != nil{ 
    let somevar3 = itemsarray[indexPath.row] 

    if aselect.contains(somevar3){ 

     let intToRemove = aselect.index(of: somevar3) 

     aselect.remove(at: intToRemove!) 
    } 

    let somevar5 = pricearray[indexPath.row] 

    if aselect.contains(somevar5){ 
     let intToRemove = aselect.index(of: somevar5) 
     aselect.remove(at: intToRemove!) 
    } 

    let thisvar2 = pricearray2[indexPath.row] 




    //error is occuring below 
    print(cell.qtyValueThing.text!) 
    value = Double(cell.qtyValueThing.text!)! 
    //error is fatal error: unexpectedly found nil while unwrapping an Optional value 


    if aselect.contains(String(value)){ 
     let intToRemove = aselect.index(of: String(value)) 
     aselect.remove(at: intToRemove!) 
    } 

    amountToSave = Double(Float(thisvar2) * Float(String(describing: value))!) 
    totalvalue = Double(Float(totalvalue) - Float(amountToSave)) 



    let totalvaluerounded = String(format: "%.2f", totalvalue) 

    Total.title = "$" + "\(totalvaluerounded)" 

    cell.qtyValueThing.isUserInteractionEnabled = true 
    } 
} 

的關鍵是使VAR非可選也檢查對象cell.qtyValueThing.text有價值

相關問題