Search code examples
iosswiftuitableviewnsindexpathindexpath

crash while deleting rows from tableview


Please check in response data delete successfully but not removed from tableview and also when i run again then data not show beacuse its delePelase check here i am deleting on yes button clickHello i am i am deleting rows from tableview and its work fine for me i am successfully removed but after remove i am getting crash and error like below

Error

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

and here i show you my code where i am getting crash

Code

@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()
            }
    }
}

i am not able to understand why i am getting crash can any one please tell me where i am done something wrong

Code For Showing Dialogue

 @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
        }

    }

Solution

  • You can define a property to save the selected indexPath before pop up the dialogue.

    var selectedIndexPath: IndexPath?
    

    And change this

    let indexPath = self.tblOccList.indexPathForRow(at: p)
    
            if indexPath == nil {
                print("Long press on table view, not row.")
            }
    

    To

    self.selectedIndexPath = self.tblOccList.indexPathForRow(at: p)
    
            if self.indexPath == nil {
                print("Long press on table view, not row.")
            }
    

    Then try changing this

    let indexPath = self.tblOccList.indexPathForRow(at: buttonPosition)!
    

    to

    guard let indexPath = self.selectedIndexPath else { return }