I'm trying to get a UIPickerView inside a custom UITableViewCell to load. The cell loads fine, and I'm able to pass the data (an array of strings) from the UITableViewController to the custom cell through a delegate but the picker just loads blank. I can't for the life of me figure out why.
This seems to be a similar issue but the suggested solution (namely to reload the components of the UIPickerView after setting the pickerData array in the cellForRowAt method) doesn't seem to work.
Any help or insights greatly appreciated.
Custom Cell with UIPickerView
class GradeSelectionCell: UITableViewCell, UIPickerViewDataSource, UIPickerViewDelegate, ReusableView {
@IBOutlet var pickerView: UIPickerView!
var pickerData = [String]()
var numComponents = Int()
override func awakeFromNib() {
// Initialization code
self.pickerData = Array<String>()
self.pickerView.delegate = self
self.pickerView.dataSource = self
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return numComponents
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
print(pickerData[row])
return pickerData[row]
}
}
extension GradeSelectionCell: PickerSetupDelegate {
func setupPicker(data: [String], numberOfCompents: Int){
pickerData = data
numComponents = numberOfCompents
print("PickerSetupDelegate Called")
print("pickerData \(String(describing: pickerData))")
}
}
cellForRowAt
let cell = tableView.dequeueReusableCell(withIdentifier: "GradeSelectionCell", for: indexPath) as! GradeSelectionCell
cell.setupPicker(data: grades, numberOfCompents: 1)
// cell.pickerView.reloadAllComponents()
return cell
Registering Cell in viewDidLoad
tableView.register(GradeSelectionCell.self, forCellReuseIdentifier: "GradeSelectionCell")