2

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 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

It works as you propose. Thank you Icaro. I have to wait 7 minutes to accept your answer.... :)
@mvasco Glad to here! Good luck with your app! (and thanks for your vote)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.