【发布时间】:2017-05-25 23:22:31
【问题描述】:
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 weather the cell is被选中或取消选中。但是,每次我deselect 一个单元格时,应用程序都会崩溃。
我遇到错误“致命错误:意外发现 nil while 展开可选值”我认为两者之间存在差异 这两个函数,但我找不到。
任何帮助将不胜感激!
谢谢,
这是我的代码:
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)"
}
【问题讨论】:
标签: ios swift3 tableview xcode8