Sort items via NSDate and add to UITableView

Before I even start asking my question, I would like to add a small note - I do not ask to enter the code. I want to directly point out that now, as some may perceive this question as a code request. I ask for ideas on HOW to do this, I don’t want anyone else to do this for me.

I recently got into Core Data, and I had one unique problem that I am having serious problems with. I have some subclasses of NSManagedObject and they have three properties, one of which is related to this situation - dueDate, which is of type NSDate . I worked on finding a way to sort the subclass objects, and then add to one of five different sections in the UITableView , which include:

  • Today
  • Tomorrow
  • One week
  • One month
  • More than one month

Sorting elements works with this function:

 func fetchAssignments() { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let managedContext = appDelegate.managedObjectContext! let fetchRequest = NSFetchRequest(entityName:"Assignment") var error: NSError? let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as! [NSManagedObject]? if let results = fetchedResults { let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! let todayDate = calendar.startOfDayForDate(NSDate()) for item in results { let dueDate = item.valueForKey("dueDate") as! NSDate println(dueDate) let calendarComponents = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay, fromDate: todayDate, toDate: dueDate, options: nil) println("Months: " + calendarComponents.month.description) println("Days: " + calendarComponents.day.description) if calendarComponents.month >= 2 || (calendarComponents.month == 1 && calendarComponents.day > 0) { println("Greater") } else if calendarComponents.month == 1 || calendarComponents.day > 7 { println("One Month") } else if calendarComponents.day > 1 { println("One Week") } else if calendarComponents.day == 1 { println("Tomorrow") } else { println("Today") } } println() tableView.reloadData() } else { println("Could not fetch \(error), \(error!.userInfo)") } } 

This function does not currently use my subclass object, but this should have no effect other than having to pass the value as an NSDate object.

My question is how to insert these objects into a UITableView . I understand that this is a fairly broad question, but I honestly don't know what else to do. I tried to use several arrays, one for each category, but it did not work out well - if one of the arrays is empty, problems arise. How can I sort these objects so that they can be inserted once in a UITableView ?

+5
source share
2 answers

I would think a little about changing your logic a bit and using a given result handler to handle sectioning.

To use the selected result controller, create a selection and add a sort descriptor by date. This will be a simple list that you can work with first.

Then add the transient property to your subclass and there you should return the string for which the section belongs to the object (the result can be cached so that you do not recount it all the time). Set the name of this transient property as the key path of the section name in FRC.

+2
source

I would recommend you study the NSFetchedResultsController class. It is specifically designed to display Core Data in a table format.

NSFetchedResultsController

You can add sorting descriptors that will sort and sort, as well as delete swipe to delete functions. It does a lot of hard work for you when it comes to managing master data in tabular form.

+1
source

All Articles