Populating UIPickerView with Core Data objects using Swift

2.6k views Asked by At

I am trying to populate an UIPickerView with Core Data objects in a Swift app.

For now, I am getting a picker view with 4 rows (there are 4 objects in the Core Data entity). My issue is that all 4 row titles are from the same object. Please take a look at a screenshot:

enter image description here

And this is my current piece of code:

override func viewWillAppear(animated: Bool) {
    
    getFetchedResultController_cat(currentEntity_cat)
}


func getFetchedResultController_cat(selectedEntity: NSString){
    
    let appDelegate : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedObjectContext: NSManagedObjectContext = appDelegate.managedObjectContext!
    
    let fetchRequest = NSFetchRequest()
    
    let entity = NSEntityDescription.entityForName(selectedEntity as String, inManagedObjectContext: managedObjectContext)
    
    fetchRequest.entity = entity
    
    fetchRequest.fetchBatchSize = 20
 
    let sectionSortDescriptor = NSSortDescriptor(key: "name", ascending: true)
    
    let sortDescriptors = [sectionSortDescriptor] //, secondSortDescriptor]
    
    fetchRequest.sortDescriptors = sortDescriptors
    
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
    
    aFetchedResultsController.delegate = self
    self.fetchedResultsController_cat = aFetchedResultsController
    
    var error: NSError? = nil
    if !self.fetchedResultsController_cat.performFetch(&error) {
        abort()
    }
    let x : Int = (self.fetchedResultsController_cat.fetchedObjects?.count ?? 0)
    println("CATEGORIAS ACTUALES=")
    println(x)
    self.pickerView.reloadAllComponents()
}

func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
    return 100
}
//size the components of the UIPickerView
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    return 20.0
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return fetchedResultsController_cat.fetchedObjects?.count ?? 0
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
  
    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    let fetchedObject: AnyObject = self.fetchedResultsController_cat.objectAtIndexPath(indexPath)
    print (fetchedObject.valueForKey("name"))
    var nombre: AnyObject? = fetchedObject.valueForKey("name")
    return nombre as! String
}

I would like to show the right row title on each row.

What am I missing in my code?

Thank you.

1

There are 1 answers

2
Icaro On BEST ANSWER

It seems like you are always retrieving the first element in the fetch result:

Try change this:

let indexPath = NSIndexPath(forRow: 0, inSection: 0) 

To this:

let indexPath = NSIndexPath(forRow: row, inSection: 0)