【问题标题】:Populating UIPickerView with Core Data objects using Swift使用 Swift 使用 Core Data 对象填充 UIPickerView
【发布时间】:2015-06-11 03:50:55
【问题描述】:

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

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

这是我目前的一段代码:

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
}

我想在每一行显示正确的行标题。

我的代码中缺少什么?

谢谢。

【问题讨论】:

    标签: ios swift uipickerview


    【解决方案1】:

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

    尝试改变这个:

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

    到这里:

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

    【讨论】:

    • 它按照您的建议工作。谢谢伊卡罗。我必须等待 7 分钟才能接受您的回答.... :)
    • @mvasco 很高兴来到这里!祝你的应用好运! (感谢您的投票)
    猜你喜欢
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    相关资源
    最近更新 更多