1

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

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

提醒表.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!]

        }
    }

提醒.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 开发的新手

提前致谢

在此处输入图像描述

4

1 回答 1

0

你还没有向我们展示一段重要的代码,你在 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 上的代码后,我对其进行了更新,并设法获得了更新更改的提醒并将其显示在表格视图中。

需要的主要改变是:

// 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) 传递给 editVC,因此不需要再次获取它。所以你只需更新值然后保存。

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

于 2016-04-05T22:27:10.963 回答