从tableview删除行时崩溃

时间:2019-01-18 09:42:11

标签: ios swift uitableview nsindexpath indexpath

Please check in response data delete successfully but not removed from tableview and also when i run again then data not show beacuse its dele Pelase check here i am deleting on yes button click你好,我正在从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
        }

    }

2 个答案:

答案 0 :(得分:2)

您可以定义一个属性,以在弹出对话框之前保存所选的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 }

答案 1 :(得分:0)

在表视图中添加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()
            }
    }
}