我正在开发提醒应用程序,我正面临一些问题,其中一个问题是当用户点击其中一个类别时,表格将根据所选类别重新加载;任务名称和类别正确重新加载,没有任何问题,但时间和日期不会被过滤,如下面的照片所示
我相信下面的代码有问题,我怎么能解决这个问题?
ReminderTable.swift
class ReminderTable: UITableViewController,NSFetchedResultsControllerDelegate{
@IBOutlet weak var menuViewRelative: UIView!
@IBOutlet weak var categroyFilter:UISegmentedControl!
var reminders:[ReminderData] = []
var fetchResultController:NSFetchedResultsController!
let reminderFetch = NSFetchRequest(entityName: "Reminder")
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
override func viewDidLoad() {
super.viewDidLoad()
title = "Reminder"
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
let fetchRequest = NSFetchRequest(entityName: "Reminder")
let sortDescriptor = NSSortDescriptor(key:"time", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
fetchResultController.delegate = self
do {
try fetchResultController.performFetch()
reminders = fetchResultController.fetchedObjects as! [ReminderData]
} catch {
print(error)
}
}
loadData("All")
tableView.allowsSelection = false
self.navigationController!.toolbarHidden = false
}
override func viewDidDisappear(animated: Bool) {
categroyFilter.selectedSegmentIndex = 0
}
func loadData(category:String){
if category == "All"{
do{
let request = NSFetchRequest(entityName:"Reminder")
let sortDescriptor = NSSortDescriptor(key:"time", ascending: true)
request.sortDescriptors = [sortDescriptor]
reminders = try managedObjectContext.executeFetchRequest(request) as! [ReminderData]
self.tableView.reloadData()
}catch{
fatalError("error")
}
}else{
do{
let request = NSFetchRequest(entityName:"Reminder")
request.predicate = NSPredicate(format: "category = %@", category)
reminders = try managedObjectContext.executeFetchRequest(request) as! [ReminderData]
self.tableView.reloadData()
}catch{
fatalError("error")
}
}
self.tableView.reloadData()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.hidesBarsOnSwipe = false
self.navigationController!.navigationBarHidden = false
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return reminders.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let task = fetchResultController.objectAtIndexPath(indexPath)
as! ReminderData
let cellIdentifier = "cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ReminderListCell
cell.name.text = reminders[indexPath.row].name
cell.category.text = reminders[indexPath.row].category
cell.date.text = task.stringForDate()
cell.time.text = task.stringForTime()
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
reminders.removeAtIndex(indexPath.row)
}
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let RemoveAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Remove",handler: { (action, indexPath) -> Void in
if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
let remove = UIAlertController(title: "Are You Sure You Want Delete this Reminder ?", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
let Yes = UIAlertAction(title: "Yes", style: .Default) { (action) in
let reminderToDelete = self.fetchResultController.objectAtIndexPath(indexPath) as! ReminderData
managedObjectContext.deleteObject(reminderToDelete)
do {
try managedObjectContext.save()
} catch {
print(error)
}
}
remove.addAction(Yes)
let No = UIAlertAction(title: "No", style: .Destructive) { (action) in
}
remove.addAction(No)
self.presentViewController(remove, animated: true) {}
}
})
let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Edit",handler: { (action, indexPath) -> Void in
tableView.setEditing(false, animated: true)
let cell = tableView.cellForRowAtIndexPath(indexPath)
self.performSegueWithIdentifier("editReminder", sender: cell)
})
RemoveAction.backgroundColor = UIColor(red: 247/255, green: 93/255, blue: 89/255, alpha: 1)
editAction.backgroundColor = UIColor(red: 21/255, green: 137/255, blue: 255/255, alpha: 1)
return [RemoveAction,editAction]
}
@IBAction func unwindToHomeScreen(segue:UIStoryboardSegue) {
}
func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
// 1
switch type {
case .Insert:
tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Automatic)
case .Delete:
tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Automatic)
case .Update:
tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Automatic)
default: break
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "editReminder" {
// let task = segue.destinationViewController as! UINavigationController
let navController = segue.destinationViewController as! UINavigationController
let task = navController.topViewController as! Reminder
if let indexPath = tableView.indexPathForCell(sender as! ReminderListCell) {
task.reminders = reminders[indexPath.row]
}
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
if let _newIndexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([_newIndexPath], withRowAnimation: .Fade)
}
case .Delete:
if let _indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([_indexPath], withRowAnimation: .Left)
}
case .Update:
if let _indexPath = indexPath {
tableView.reloadRowsAtIndexPaths([_indexPath], withRowAnimation: .Fade)
}
default:
tableView.reloadData()
}
reminders = controller.fetchedObjects as! [ReminderData]
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}
@IBAction func selectCategory(sender: UISegmentedControl) {
let selectedValue = sender.titleForSegmentAtIndex(sender.selectedSegmentIndex)
switch selectedValue!{
case "Task":
loadData(selectedValue!)
break
case "Medication":
loadData(selectedValue!)
break
case "Appointment":
loadData(selectedValue!)
break
default:
loadData(selectedValue!)
break
}
}
override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
var fixedFrame:CGRect = self.menuViewRelative.frame
fixedFrame.origin.y = 0 + scrollView.contentOffset.y
self.menuViewRelative.frame = fixedFrame
}
}
ReminderData.swift
class ReminderData : NSManagedObject {
@NSManaged var date: NSDate?
@NSManaged var name: String?
@NSManaged var category: String?
@NSManaged var time : NSDate?
func stringForDate() -> String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
if let date = date {
return dateFormatter.stringFromDate(date)
} else {
return ""
}
}
func stringForTime() -> String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh:mm a"
if let time = time {
return dateFormatter.stringFromDate(time)
} else {
return ""
}
}
}
答案 0 :(得分:1)
替换
task.stringForDate()
task.stringForTime()
带
reminders[indexPath.row].stringForDate()
reminders[indexPath.row].stringForTime()