How can I populate a tableView using NSSet? (Fast)

I have a Person class in tableView1, then in TableView2 the user adds brands to communicate with the person. My fetch query is trying to return the person they selected and populate the tableView rows with that person's respective brands, but I'm stuck here:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath) as! UITableViewCell

        let brandSetAllObjects = brandSet?.allObjects


        let brand = brandSetAllObjects as? Brand[indexPath.row]
        cell.textLabel!.text = brand.valueForKey("name") as? String


        return cell
    }

      

Fetch:

 //Helper Function to Fetch Core Data
    func fetchCoreData() {
        //1
        let appDelegate =
        UIApplication.sharedApplication().delegate as! AppDelegate

        let managedContext = appDelegate.managedObjectContext!

        //2
        let fetchRequest = NSFetchRequest(entityName:"Person")


        //3
        var error: NSError?

        let fetchedResults =
        managedContext.executeFetchRequest(fetchRequest,
            error: &error) as? [NSManagedObject]

        if let results = fetchedResults as? [Person] {
            for selectedPerson in results {
                brandSet = selectedPerson.brands
                println(brandSet)
            }

        } else {
            println("Could not fetch \(error), \(error!.userInfo)")
        }

      

classes:

import Foundation
import CoreData

class Brand: NSManagedObject {

    @NSManaged var name: String
    @NSManaged var people: NSSet


}

import Foundation
import CoreData

class Person: NSManagedObject {

    @NSManaged var name: String
    @NSManaged var brands: NSSet

    func addBrandsObject(value: Brand) {
        self.mutableSetValueForKey("brands").addObject(value)
    }

}

      

+3


source to share


1 answer


Is there a specific sort order that you want to display in the table view?

Instead of directly accessing the data structure, " Brand

" you can have a data manager that treats the data in a consistent manner. You can store all the information Brand

in NSSet

, and then have a method in your data manager that returns NSArray

(possibly called brandArray

). The data manager could take the contents NSSet

, add them to an array, and then sort the array so that they are in uniform order, and return that array.

On the side, UITableView

just replace Brand[indexPath.row]

with something like dataManager.brandArray[indexPath.row]

.



My suggestion for the data manager would be to make the class to be implemented as a singleton . Step 4 of this tutorial explains quite well how to set up the Data Manager.

Side note: the data manager is just a concept, not a strict class or object type. It can be any object that manipulates data, although it is usually run as a singleton that can be accessed from any other class in your project.

+1


source







All Articles