UIPickerView数据加载异常求助:行数正确但显示问号
Hey there! As someone who’s fumbled through early Swift picker view issues myself, let me break down exactly what’s going on here and how to fix it.
The Core Issue: Missing the titleForRow Data Source Method
UIPickerView shows "?" when it doesn’t get explicit text to display for each row. You’ve correctly implemented the methods that tell the picker how many components and rows there are, but you’re missing the critical method that supplies the actual content for each row.
Step-by-Step Fixes
Add the
titleForRowmethod
This is the key piece you’re missing. It tells the picker what text to show for every row. Make sure to reference yourpickerValuesarray here:func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return pickerValues[row] }Double-check your
pickerValuesarray
Ensure the array is properly initialized in your view controller, and that its count matches the number of rows you’re returning (4 in your code). For example:// Add this at the top of your ViewController3 class let pickerValues = ["Option 1", "Option 2", "Option 3", "Option 4"]Pro tip: Instead of hardcoding
return 4innumberOfRowsInComponent, usereturn pickerValues.count—this keeps your row count in sync with your array automatically.Confirm delegate/dataSource connections
- If you created the picker in Storyboard/XIB: Open the Storyboard, control-drag from the UIPickerView to your ViewController3’s yellow "View Controller" icon, and select both
delegateanddataSource. - If you created the picker in code: Make sure you set these properties when initializing the picker:
let pickerView = UIPickerView() pickerView.delegate = self pickerView.dataSource = self // Don't forget to add it to your view! view.addSubview(pickerView)
- If you created the picker in Storyboard/XIB: Open the Storyboard, control-drag from the UIPickerView to your ViewController3’s yellow "View Controller" icon, and select both
Full Corrected Code Example
Here’s how your complete ViewController3 should look with all fixes applied:
import UIKit class ViewController3: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { // Initialize your value array let pickerValues = ["First Choice", "Second Choice", "Third Choice", "Fourth Choice"] override func viewDidLoad() { super.viewDidLoad() // If using code-created picker, uncomment and adjust the frame below // let pickerView = UIPickerView() // pickerView.frame = CGRect(x: 0, y: 150, width: view.frame.width, height: 200) // pickerView.delegate = self // pickerView.dataSource = self // view.addSubview(pickerView) } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { // Use array count instead of hardcoding return pickerValues.count } // The missing method that fixes the "?" issue func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return pickerValues[row] } }
内容的提问来源于stack exchange,提问作者Alan

