表格视图中 select 和 deselect 单元格之间的差异

Discrepancy between select and deselect cells in tableview

I am creating an app which involves the use of a tableview, when a cell is selected the qty value is times by the price and then that value is added or subtracted from the total sum, depending on天气单元格被选中或取消选中。但是,每次我 deselect 一个单元格时,应用程序都会崩溃。

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)"
}

我想通了,就这样

 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 是否有值