我正在构建提醒应用,当我向左侧交换时,我可以删除并更新提醒;当我按下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开发中的新手
提前致谢
答案 0 :(得分:0)
您没有向我们展示一段重要的代码,您在Reminder View Controller中做了什么?
从我可以看到你在Reminder 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(提醒)传递给editVC,因此无需再次获取它。所以你只需更新值然后保存。
此外,当您提取时,您正在获取所有记录。如果您想这样做,您可能会将提醒ID传递给Reminder VC,然后按ID加载,修改并保存。为了做到这一点,你需要添加一个ID字段并更新你的fetch以包含一个谓词,但是现在你不需要它。