0

Having trouble seeing through my mistake here. I'm fetching records from CoreData into Array and having trouble having all records show up in UIPickerView. I'm sure i'm missing something very simple but just started to learn Swift and looking for some guidance.

Expected Result: I have 2 players in my fetch request and both players should show up in the UIPickerView

Actual Result: Only one record shows up

Code:

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

1

It seems that you are assigning a new array to playerPickerSource each turn in your loop.

So instead of

for player in playerArray {

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

You should try

for player in playerArray {
    playerPickerSource.append(player.playername!)
}
Sign up to request clarification or add additional context in comments.

1 Comment

I knew I was getting tired!! Thank you so much for your help GBaeOne.

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.