UIPickerView 和 CoreData 数组

Posted

技术标签:

【中文标题】UIPickerView 和 CoreData 数组【英文标题】:UIPickerView and CoreData Array 【发布时间】:2017-10-08 23:18:15 【问题描述】:

在这里看穿我的错误时遇到了麻烦。我正在将 CoreData 中的记录提取到 Array 中,并且无法让所有记录都显示在 UIPickerView 中。我确定我错过了一些非常简单的东西,但刚刚开始学习 Swift 并寻求一些指导。

预期结果:我的 fetch 请求中有 2 个玩家,两个玩家都应该出现在 UIPickerView 中

实际结果:只显示一条记录

代码:

import UIKit
import CoreData

class selectPlayersStatsViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, NSFetchedResultsControllerDelegate 

@IBOutlet weak var playerSelector: UIPickerView!

var moc:NSManagedObjectContext!

var playerPickerSource: [String] = [String]()

override func viewDidLoad() 
    super.viewDidLoad()

    playerSelector.dataSource = self
    playerSelector.delegate = self

    moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let playerIsRequest:NSFetchRequest<PlayerTable> = PlayerTable.fetchRequest()
        playerIsRequest.returnsObjectsAsFaults = false

    var playerArray = [PlayerTable]()

    do 
        playerArray = try moc.fetch(playerIsRequest)

     catch 
        print(error)
    

   for player in playerArray 

        playerPickerSource = [player.playername!]
        //print(playerPickerSource)
        //print(playerPickerSource.count)
    



//PICKER VIEW
public func numberOfComponents(in pickerView: UIPickerView) -> Int
    return 1



public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int

    return playerPickerSource.count



public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 

    return playerPickerSource[row]



public func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView 

    let playerLabel = UILabel()
    let playerData = playerPickerSource[row]

    let myTitleYear = NSAttributedString(string: playerData, attributes: [NSFontAttributeName:UIFont(name: "Avenir Next", size: 18.0)!,NSForegroundColorAttributeName:UIColor.black])

    playerLabel.textAlignment = .center
    playerLabel.attributedText = myTitleYear
    return playerLabel


/////////
@IBAction func goViewButton(_ sender: Any) 

    let myPlayer = playerPickerSource[playerSelector.selectedRow(inComponent: 0)]
    //let myYear = yearPickerSource[playerSelector.selectedRow(inComponent: 0)]
    print(myPlayer)
   // print(myYear)



【问题讨论】:

【参考方案1】:

您似乎在循环中的每一轮都为 playerPickerSource 分配了一个新数组。

所以不是

for player in playerArray 

    playerPickerSource = [player.playername!]
    //print(playerPickerSource)
    //print(playerPickerSource.count)

你应该试试

for player in playerArray 
    playerPickerSource.append(player.playername!)

【讨论】:

我知道我累了!!非常感谢您对 GBaeOne 的帮助。

以上是关于UIPickerView 和 CoreData 数组的主要内容,如果未能解决你的问题,请参考以下文章

从 coredata 表中检索数据到 UIPickerView(在 textField 内)

UIPickerView 未填充从 CoreData 获取的数据

使用 UIPickerView 建立 CoreData 关系

将 CoreData 对象的字符串属性加载到 UIPickerView

使用 Swift 使用 Core Data 对象填充 UIPickerView

UIPickerview 和 Core Data 有关系