从表视图中删除行时崩溃
crash while deleting rows from tableview
你好,我正在从 tableview 中删除行,它对我来说工作正常,我已成功删除,但删除后出现崩溃和错误,如下所示
错误
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
在这里,我向您展示了我崩溃的代码
代码
@IBAction func btnDeleteYesTapped(_ sender: UIButton) {
let preferences = UserDefaults.standard
let uid = "u_id"
let acTkn = "acc_tkn"
let u_ID = preferences.object(forKey: uid)
let A_Token = preferences.object(forKey: acTkn)
let params = ["user_id": u_ID!, "access_token": A_Token!,"property_id": propertyID!,"occupants_id": oCCID!]
print(params)
Alamofire.request(propertyoccupantsdelete, method: .post, parameters: params).responseJSON
{
response in
print(response)
let result = response.result.value
print(response)
let data = result as! [String : AnyObject]
let status = data["success"] as! String
if status == "1"{
let buttonPosition : CGPoint = (sender as AnyObject).convert((sender as AnyObject).bounds.origin, to: self.tblOccList)
let indexPath = self.tblOccList.indexPathForRow(at: buttonPosition)! //At Here I am Getting crash
self.occuPantsData.remove(at: indexPath.row)
self.tblOccList.deleteRows(at: [indexPath], with: .fade)
Toast(text: data["message"] as? String).show()
}else{
Toast(text: data["message"] as? String).show()
}
}
}
我不明白为什么我会崩溃,谁能告诉我哪里做错了
显示对话的代码
@objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {
let p = longPressGesture.location(in: self.tblOccList)
let indexPath = self.tblOccList.indexPathForRow(at: p)
if indexPath == nil {
print("Long press on table view, not row.")
}
else if (longPressGesture.state == UIGestureRecognizer.State.began) {
print("Long press on row, at \(indexPath!.row)")
let id = occuPantsData[indexPath!.row].id
print(id)
self.oCCID = id
self.viewDeletePopUp.isHidden = false
}
}
您可以定义一个属性来保存在弹出对话框之前选择的indexPath。
var selectedIndexPath: IndexPath?
并改变这个
let indexPath = self.tblOccList.indexPathForRow(at: p)
if indexPath == nil {
print("Long press on table view, not row.")
}
到
self.selectedIndexPath = self.tblOccList.indexPathForRow(at: p)
if self.indexPath == nil {
print("Long press on table view, not row.")
}
然后尝试改变这个
let indexPath = self.tblOccList.indexPathForRow(at: buttonPosition)!
至
guard let indexPath = self.selectedIndexPath else { return }
在表视图中添加 cellForRowAt 委托方法,将按钮标签分配给 indexPath.row
cell.button.tag = indexPath.row
@IBAction func btnDeleteYesTapped(sender: UIButton) {
let preferences = UserDefaults.standard
let uid = "u_id"
let acTkn = "acc_tkn"
let u_ID = preferences.object(forKey: uid)
let A_Token = preferences.object(forKey: acTkn)
let params = ["user_id": u_ID!, "access_token": A_Token!,"property_id": propertyID!,"occupants_id": oCCID!]
print(params)
Alamofire.request(propertyoccupantsdelete, method: .post, parameters: params).responseJSON
{
response in
print(response)
let result = response.result.value
print(response)
let data = result as! [String : AnyObject]
let status = data["success"] as! String
if status == "1"{
let indexPath = IndexPath(row: sender.tag, section: 0)
self.occuPantsData.remove(at: indexPath.row)
self.tblOccList.reloadData()
Toast(text: data["message"] as? String).show()
}else{
Toast(text: data["message"] as? String).show()
}
}
}
错误
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
在这里,我向您展示了我崩溃的代码
代码
@IBAction func btnDeleteYesTapped(_ sender: UIButton) {
let preferences = UserDefaults.standard
let uid = "u_id"
let acTkn = "acc_tkn"
let u_ID = preferences.object(forKey: uid)
let A_Token = preferences.object(forKey: acTkn)
let params = ["user_id": u_ID!, "access_token": A_Token!,"property_id": propertyID!,"occupants_id": oCCID!]
print(params)
Alamofire.request(propertyoccupantsdelete, method: .post, parameters: params).responseJSON
{
response in
print(response)
let result = response.result.value
print(response)
let data = result as! [String : AnyObject]
let status = data["success"] as! String
if status == "1"{
let buttonPosition : CGPoint = (sender as AnyObject).convert((sender as AnyObject).bounds.origin, to: self.tblOccList)
let indexPath = self.tblOccList.indexPathForRow(at: buttonPosition)! //At Here I am Getting crash
self.occuPantsData.remove(at: indexPath.row)
self.tblOccList.deleteRows(at: [indexPath], with: .fade)
Toast(text: data["message"] as? String).show()
}else{
Toast(text: data["message"] as? String).show()
}
}
}
我不明白为什么我会崩溃,谁能告诉我哪里做错了
显示对话的代码
@objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {
let p = longPressGesture.location(in: self.tblOccList)
let indexPath = self.tblOccList.indexPathForRow(at: p)
if indexPath == nil {
print("Long press on table view, not row.")
}
else if (longPressGesture.state == UIGestureRecognizer.State.began) {
print("Long press on row, at \(indexPath!.row)")
let id = occuPantsData[indexPath!.row].id
print(id)
self.oCCID = id
self.viewDeletePopUp.isHidden = false
}
}
您可以定义一个属性来保存在弹出对话框之前选择的indexPath。
var selectedIndexPath: IndexPath?
并改变这个
let indexPath = self.tblOccList.indexPathForRow(at: p)
if indexPath == nil {
print("Long press on table view, not row.")
}
到
self.selectedIndexPath = self.tblOccList.indexPathForRow(at: p)
if self.indexPath == nil {
print("Long press on table view, not row.")
}
然后尝试改变这个
let indexPath = self.tblOccList.indexPathForRow(at: buttonPosition)!
至
guard let indexPath = self.selectedIndexPath else { return }
在表视图中添加 cellForRowAt 委托方法,将按钮标签分配给 indexPath.row
cell.button.tag = indexPath.row
@IBAction func btnDeleteYesTapped(sender: UIButton) {
let preferences = UserDefaults.standard
let uid = "u_id"
let acTkn = "acc_tkn"
let u_ID = preferences.object(forKey: uid)
let A_Token = preferences.object(forKey: acTkn)
let params = ["user_id": u_ID!, "access_token": A_Token!,"property_id": propertyID!,"occupants_id": oCCID!]
print(params)
Alamofire.request(propertyoccupantsdelete, method: .post, parameters: params).responseJSON
{
response in
print(response)
let result = response.result.value
print(response)
let data = result as! [String : AnyObject]
let status = data["success"] as! String
if status == "1"{
let indexPath = IndexPath(row: sender.tag, section: 0)
self.occuPantsData.remove(at: indexPath.row)
self.tblOccList.reloadData()
Toast(text: data["message"] as? String).show()
}else{
Toast(text: data["message"] as? String).show()
}
}
}