4

Im trying to make an app that stores personal details which can be deleted, edited using editActionsForRowAtIndexPath. The delete option seems to work fine but I am having problems with the edit action.

I get an error as I've mentioned below:

Could not cast value of type 'Table_view.UserTableViewController' (0x10a1991b0) to 'NSIndexPath' (0x10a5e8438).

UserRecordViewController is the View Controller where the personal details are to be displayed. And InsertRecordViewController is the other View Controller.

UserTableViewController relevant Code :

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    // 1
    let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Edit" , handler: { (action:UITableViewRowAction, indexPath:NSIndexPath) -> Void in

            self.performSegueWithIdentifier("editSegue", sender: self)

    })

    editAction.backgroundColor = UIColor.darkGrayColor()
   // let editIndex = editAction.indexOfAccessibilityElement(indexPath.row)

    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction, indexPath:NSIndexPath) -> Void in

        let userInfo: UserInfo = self.marrUserData.objectAtIndex(indexPath.row) as! UserInfo
        let isDeleted = ModelManager.getInstance().deleteUserData(userInfo)
        if isDeleted {
            Util.invokeAlertMethod("", strBody: "Record deleted successfully.", delegate: nil)
        } else {
            Util.invokeAlertMethod("", strBody: "Error in deleting record.", delegate: nil)
        }
        self.getUserData()

    })

    deleteAction.backgroundColor = UIColor.lightGrayColor()

    return [deleteAction, editAction]
}



override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if(segue.identifier == "editSegue"){

        let selectedIndexPath = sender as! NSIndexPath
        let index = selectedIndexPath.row
        //if let indexPath: NSIndexPath = self.tableView.indexPathForCell(sender as! UITableViewCell) {

        //let btnEdit : UIButton = sender as! UIButton
        //let selectedIndex : Int = btnEdit.tag
        let viewController : InsertRecordViewController = segue.destinationViewController as! InsertRecordViewController
        viewController.isEdit = true
        viewController.userData = self.marrUserData.objectAtIndex(index) as! UserInfo

       // }

    }

}

I would like to know where I'm going wrong. Any idea guys?

Thanks in advance!!

2 Answers2

1

I had the same exact problem and I was able to solve it by adding a few extra things in the func prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if(segue.identifier == "editSegue"){

    let cell = sender as! UITableViewCell
    let Path = tableView.indexPathForCell(cell)
    let index = Path?.row
    let viewController : InsertRecordViewController = segue.destinationViewController as! InsertRecordViewController
    viewController.isEdit = true
    viewController.userData = self.marrUserData.objectAtIndex(index!) as! UserInfo

   // }

}

Now I realize this isn't exactly what you're trying to do. Because with my method you just click on the cell and it directs you immideatly to the path instead of sliding. I wanted to do the slide as well but I couldn't figure it out so I just with with the above method. Hope this helps :)

Ozvengrad
  • 302
  • 5
  • 10
0

The line that cause the error is: let selectedIndexPath = sender as! NSIndexPath Sender is the SelectedCell, not the IndexPath!
Before the line self.performSegueWithIdentifier("editSegue", sender: self) set an property of the class (selectedIndexPath) to indexPath, and then you access this property from prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?).

Another method to do this can be found at: https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson9.html#//apple_ref/doc/uid/TP40015214-CH9-SW1

alessionossa
  • 923
  • 2
  • 15
  • 41