我正在构建提醒应用程序,当我向左切换时,我可以删除和更新提醒;当我按下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 开发的新手
提前致谢