Jenita _Alice4Real Jenita _Alice4Real - 8 months ago 35
iOS Question

Swipe to edit sqlite database content in swift

Im trying to make an app that stores personal details which can be deleted, edited using

. 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).

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

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)


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!!


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: