表格视图中选择和取消选择单元格之间的差异
Posted
技术标签:
【中文标题】表格视图中选择和取消选择单元格之间的差异【英文标题】:Discrepancy between select and deselect cells in tableview 【发布时间】: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)"
【问题讨论】:
【参考方案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 是否有值
【讨论】:
以上是关于表格视图中选择和取消选择单元格之间的差异的主要内容,如果未能解决你的问题,请参考以下文章