tableview中select和deselect单元格之间的差异

时间:2017-05-25 23:22:31

标签: ios swift3 tableview xcode8

我正在创建一个涉及使用tableview的应用,当选择一个单元格时,数量值是价格的倍数,然后根据天气的总和加上或减去该值正在选择或取消选择单元格。但是,每当我deselect一个单元格时,应用程序就会崩溃。

  

我遇到错误“致命错误:意外发现没有   展开一个可选值“我认为两者之间存在差异   这两个功能,但我找不到它。

非常感谢任何帮助!

谢谢,

继承我的代码:

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 个答案:

答案 0 :(得分: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 是否有值