Swipe to edit sqlite database contents in swift

I am trying to create an application that stores personal data that can be deleted, edited with editActionsForRowAtIndexPath. The delete option seems to work fine, but I am having problems with the editing action.

I get an error, as I mentioned below:

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

UserRecordViewController- This is a view controller in which personal data should be displayed. And InsertRecordViewController- this is another 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 am going wrong. Any ideas guys?

Thanks in advance!

+4
source share
2 answers

, : let selectedIndexPath = sender as! NSIndexPath - SelectedCell, IndexPath!
self.performSegueWithIdentifier("editSegue", sender: self) (selectedIndexPath) indexPath, prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?).

: https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson9.html#//apple_ref/doc/uid/TP40015214-CH9-SW1

0

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

   // }

}

, , . , , . , , . , :)

+1

Source: https://habr.com/ru/post/1651749/


All Articles