滑动以在swift中编辑sqlite数据库内容

时间:2016-08-19 07:32:40

标签: ios uitableview swift2

我正在尝试创建一个应用程序,用于存储可以删除的个人详细信息,使用editActionsForRowAtIndexPath进行编辑。删除选项似乎工作正常,但我遇到了编辑操作的问题。

我收到错误,如下所述:

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

UserRecordViewController是视图控制器,其中显示个人详细信息。 InsertRecordViewController是另一个视图控制器。

UserTableViewController相关代码:

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

       // }

    }

}

我想知道我哪里出错了。任何想法的人?

提前致谢!!

2 个答案:

答案 0 :(得分:1)

我有同样的问题,我可以通过在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 :(得分:0)

导致错误的行是:let selectedIndexPath = sender as! NSIndexPath Sender是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

找到