如何使用 swift 中的行操作从数据库加载数据

how I can load a data from database with row actions in swift

我正在构建提醒应用程序,当我向左切换时,我可以删除和更新提醒;当我按如下所示的 edit 按钮时,它假设从数据库加载数据,因此我可以编辑和更新数据,但它就像创建一个新的 reminder.

以及如何调整行操作的大小。

ReminderTable.swift

  override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

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


            if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {

                let reminderToDelete = self.fetchResultController.objectAtIndexPath(indexPath) as! ReminderData
                managedObjectContext.deleteObject(reminderToDelete)

                do {
                    try managedObjectContext.save()
                } catch {
                    print(error)
                }
            }
        })


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

                let st:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let vc :UINavigationController = st.instantiateViewControllerWithIdentifier("Reminder") as! UINavigationController
                self.presentViewController(vc, animated: true, completion: nil)

        })


        deleteAction.backgroundColor = UIColor.redColor()
        editAction.backgroundColor = UIColor.blueColor()
        return [deleteAction,editAction]
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Reminder" {

            let task = segue.destinationViewController as! Reminder


            let indexpath = self.tableView.indexPathForSelectedRow

            let row = indexpath?.row

            task.reminders = reminders[row!]

        }
    }

Reminder.swift

    override func viewDidLoad() {
        super.viewDidLoad()

    dateTextField.delegate = self
    nameTextField.delegate = self



    if let reminderContent = reminders

    {

        nameTextField.text = reminderContent.name
      dateTextField.text = reminderContent.stringForDate()
        timePick.date = reminderContent.time!
      }  

        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy"
        dateFormatter.stringFromDate(reminderContent.date!)

        let timeFormatter = NSDateFormatter()
        timeFormatter.dateFormat = "hh:mma"
            timeFormatter.stringFromDate(reminderContent.time!)

    }

 func inserte(){

        let storingName = nameTextField.text

        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy"
        let date = dateFormatter.dateFromString(dateTextField.text!)

        if reminders == nil {

            if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
                reminders = NSEntityDescription.insertNewObjectForEntityForName("Reminder", inManagedObjectContext: managedObjectContext) as? ReminderData

                reminders!.name = storingName!
                reminders.date = date
                reminders.time = timePick.date



                do {
                    try managedObjectContext.save()
                } catch {
                    print(error)
                    return
                }
            }


        }
    }

请注意,我在 iOS 开发

方面还是个新手

提前致谢

enter image description here

你没有向我们展示一段重要的代码,你在 Reminder View Controller 中做什么?

据我所知,您正在将提醒 VC 上的提醒 属性 设置到所选行,此代码 运行 是否符合预期?

进入提醒视图控制器后,我猜您有一个表单,您是否在 viewDidLoad/viewDidAppear 中设置属性?

我希望看到类似的内容:

func viewDidLoad(animated:Bool) {
    Super.viewDidLoad(animated:animated)

    DescriptionTextField.text = reminder.description
    LocationTextField.text = reminder.location
    // ... And so on, filling out each property on your model
}

此功能不是内置的,因此您需要构建表单并处理数据验证并保存到托管对象上下文中(确保您在同一线程上执行此操作)

查看 github 上的代码后,我更新了它并设法获得更新更改的提醒并在 table 视图中显示。

需要进行的主要更改是:

// near the top of the file..
var reminder:ReminderData?

// then further down
func update(){

    let storingName = nameTextField.text
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MM-dd-yyyy"
    let date = dateFormatter.dateFromString(dateTextField.text!)


    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext


    if let reminder = reminder {
        reminder.name = storingName
        if let categoryStorage = storingCategory {
            reminder.category = categoryStorage
        }

        reminder.date = date
        reminder.time = timePick.date

        do {
            try managedObjectContext.save()

        } catch let error as NSError {
            print("ERROR in update: \(error)")
        }
    }

    self.navigationController?.popViewControllerAnimated(true)

}

这里的主要变化是不再尝试获取提醒,您已将 managedObject(Reminder) 传递给编辑VC,因此不需要再次获取它。所以你只需更新值然后保存。

此外,当您获取时,您正在获取所有记录。如果你想这样做,你可能会将提醒 ID 传递给 Reminder VC,然后通过 ID 加载它,修改并保存。为此,您需要添加一个 ID 字段并更新您的提取以包含一个谓词,但目前您不需要它。