在使用自定义按钮手动删除项目时出现问题,当用户单击该变量的负号时该项目的数量(单元格的变量)==1。正确的单元格随着上一个被删除而向上滑动,但是当下面的所有单元格都上升时,即使我正确地从字典中删除了该单元格和该值,索引也不会改变。
我试图无法在屏幕上进行用户活动,但是仍然不是问题,因为我知道这是类型索引超出范围的错误。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
ordersTableView.register(UINib(nibName: "OrdersTableViewCell", bundle: .main), forCellReuseIdentifier: "ordersTableViewCell")
let cell = ordersTableView.dequeueReusableCell(withIdentifier: "ordersTableViewCell", for: indexPath) as! OrdersTableViewCell
let amount = dishesDictionary[indexPath.item]![4] as! Int
cell.dishAmountLabel.text = ("\(String(amount))x")
@objc func decreaseAmount(sender: UIButton){
let index = sender.tag
let indexPath = IndexPath(item: index, section: 0)
if (dishesDictionary[index]![4] as! Int) > 1 {
let numberOfItems = dishesDictionary[index]![4] as! Int
dishesDictionary[index]![4] = numberOfItems - 1
ordersTableView.reloadRows(at: [indexPath], with: .fade)
print(dishesDictionary[index])
print("COUNT: \(dishesDictionary.count)")
}
else if (dishesDictionary[index]![4] as! Int) == 1 {
ordersTableView.beginUpdates()
self.dishesDictionary.removeValue(forKey: index)
self.ordersTableView.deleteRows(at: [indexPath], with: .fade)
ordersTableView.endUpdates()
UIApplication.shared.beginIgnoringInteractionEvents()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: {
self.ordersTableView.reloadData()
UIApplication.shared.endIgnoringInteractionEvents()
})
}
else {
return
}
calculateTotalAmount()
}