Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort Elements via NSDate and add to UITableView

Before I even start asking my question, I'd like to add a little note - I'm not asking for code. I want to explicitly state that now, as some may perceive this question as a request for code. I'm asking for ideas on HOW to achieve this, I don't want someone else to do so for me.

Recently, I've gotten myself into Core Data, and I've been experiencing one unique issue that I'm having major problems solving. I have some objects subclassing NSManagedObject, and they have three properties, one of which is relevant to this situation - dueDate, which is of the type NSDate. I've been working to find a way to sort the subclassing objects, and then add then into one of five different sections in the UITableView, which include:

  1. Today
  2. Tomorrow
  3. One Week
  4. One Month
  5. Greater Than One Month

Sorting the 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)")
    }

}

Currently, this function does not utilize my subclassing object, but that shouldn't have any effect other than the need to cast the value as a NSDate object.

My question is, how can I insert this objects into the UITableView. I understand this is quite a broad question, but I honestly do not know what else to do. I've tried using multiple arrays, one for each category, but that hasn't worked out to well - if one of the arrays is empty, problems arise. How can I sort these objects so that they can inserted and deleted once in the UITableView?

like image 742
Jeffrey Avatar asked May 02 '15 18:05

Jeffrey


2 Answers

I'd look at moving your logic around a little, and at using a fetched results controller to deal with the sectioning.

To use a fetched results controller create a fetch and add a sort descriptor by date. This will be a plain list that you can get working first.

Then, add a transient property to you subclass and there you should return the string for which section the object belongs in (the result can be cached so you aren't recalculating it all the time). Set the name of this transient property as the section name key path on the FRC.

like image 73
Wain Avatar answered Oct 18 '22 08:10

Wain


I'd recommend you look into the NSFetchedResultsController class. It's specifically designed to display Core Data into a tableview.

NSFetchedResultsController

You can add sort descriptors, which will keep your sorted and organized and supports the swipe left to delete functions. It does a lot of heavy lifting for you when it comes to managing core data in a table view.

like image 27
Garret Avatar answered Oct 18 '22 07:10

Garret