使用 Swift 使用 Core Data 对象填充 UIPickerView [英] Populating UIPickerView with Core Data objects using Swift

查看:26
本文介绍了使用 Swift 使用 Core Data 对象填充 UIPickerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Swift 应用程序中使用 Core Data 对象填充 UIPickerView.

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

现在,我得到了一个包含 4 行的选择器视图(Core Data 实体中有 4 个对象).我的问题是所有 4 行标题都来自同一个对象.请看截图:

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:

这是我目前的代码:

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.

我的代码中缺少什么?

谢谢.

推荐答案

似乎您总是在获取获取结果中的第一个元素:

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

试试改变这个:

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

为此:

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

这篇关于使用 Swift 使用 Core Data 对象填充 UIPickerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆