UIPickerView 在滚动时跳过数组中的值

Posted

技术标签:

【中文标题】UIPickerView 在滚动时跳过数组中的值【英文标题】:UIPickerView jumps over values in array while scroll it 【发布时间】:2018-06-09 18:10:21 【问题描述】:

我遇到了 UIPickerView 的问题。我有一个应用程序,其中 UITextField 的 inputView 设置为 UIPickerView。当它出现时,一切正常。该行的标题是正确的,但是当我向下滚动到下一个元素时,它以某种方式传递了数组的一些值并停在错误的值上。最后,标题不等于它的实际值。

这是我的代码:

class ViewController: UIViewController 

...

// MARK: - Race outlet
@IBOutlet weak var raceTextField: UITextField!

// MARK: - "Purchased" column outlets
@IBOutlet weak var purchasedStrTextField: UITextField!
@IBOutlet weak var purchasedDexTextField: UITextField!
@IBOutlet weak var purchasedConTextField: UITextField!
@IBOutlet weak var purchasedIntTextField: UITextField!
@IBOutlet weak var purchasedWisTextField: UITextField!
@IBOutlet weak var purchasedChaTextField: UITextField!

...

// MARK: - "Actual" column outlets
@IBOutlet weak var actualStrLabel: UILabel!
@IBOutlet weak var actualDexLabel: UILabel!
@IBOutlet weak var actualConLabel: UILabel!
@IBOutlet weak var actualIntLabel: UILabel!
@IBOutlet weak var actualWisLabel: UILabel!
@IBOutlet weak var actualChaLabel: UILabel!

...

// MARK: - Constants
let pickerView = UIPickerView()

// MARK: - Variables
var raceArray = [Race]()
var selectedTextField: Int!

// MARK: - Lifecicle
override func viewDidLoad() 
    super.viewDidLoad()

    configurePickerView()
    congigureTextFields()
    createRaces()

...


...

// MARK: - Methods

func createRaces() 
    let human = Race(str: 0, dex: 0, con: 0, int: 0, wis: 0, cha: 0,
                     raceDesrciption: "Human (add +2 to any one ability)", raceMod: true)

    let elf = Race(str: 0, dex: 2, con: -2, int: 2, wis: 0, cha: 0,
                   raceDesrciption: "Elf", raceMod: false)

    let halfElf = Race(str: 0, dex: 0, con: 0, int: 0, wis: 0, cha: 0,
                       raceDesrciption: "Half-Elf (add +2 to any one ability)", raceMod: true)

    let dwarf = Race(str: 0, dex: 0, con: 2, int: 0, wis: 2, cha: -2,
                     raceDesrciption: "Dwarf", raceMod: false)

    let hafling = Race(str: -2, dex: 2, con: 0, int: 0, wis: 0, cha: 2,
                       raceDesrciption: "Hafling", raceMod: false)

    let gnome = Race(str: -2, dex: 0, con: 2, int: 0, wis: 0, cha: 2,
                     raceDesrciption: "Gnome", raceMod: false)

    let halfOrc = Race(str: 0, dex: 0, con: 0, int: 0, wis: 0, cha: 0,
                       raceDesrciption: "Half-Orc (add +2 to any one ability)", raceMod: true)

    let other = Race(str: 0, dex: 0, con: 0, int: 0, wis: 0, cha: 0,
                     raceDesrciption: "Other", raceMod: true)

    raceArray.append(human)
    raceArray.append(elf)
    raceArray.append(halfElf)
    raceArray.append(dwarf)
    raceArray.append(hafling)
    raceArray.append(gnome)
    raceArray.append(halfOrc)
    raceArray.append(other)


/// Set parameters for specific race
func setDefaultPoints(_ selectedRace: String) 
    for item in raceArray 
        if selectedRace == item.raceDesrciption 
            raceModeStrTextField.text = "\(item.str!)"
            raceModeDexTextField.text = "\(item.dex!)"
            raceModeConTextField.text = "\(item.con!)"
            raceModeIntTextField.text = "\(item.int!)"
            raceModeWisTextField.text = "\(item.wis!)"
            raceModeChaTextField.text = "\(item.cha!)"

            setActualPointsForRace(item)
            switchRaceModTextTield(item.editableRaceMod)
        
    


/// Update "Actual points" labels
func setActualPointsForRace(_ race: Race) 
    actualStrLabel.text = "\(Int(lowPoweredValueLabel.text!)! - race.str)"
    strLabel.text = actualStrLabel.text

    actualDexLabel.text = "\(Int(lowPoweredValueLabel.text!)! - race.dex)"
    dexLabel.text = actualDexLabel.text

    actualConLabel.text = "\(Int(lowPoweredValueLabel.text!)! - race.con)"
    conLabel.text = actualConLabel.text

    actualIntLabel.text = "\(Int(lowPoweredValueLabel.text!)! - race.int)"
    intLabel.text = actualIntLabel.text

    actualWisLabel.text = "\(Int(lowPoweredValueLabel.text!)! - race.wis)"
    wisLabel.text = actualWisLabel.text

    actualChaLabel.text = "\(Int(lowPoweredValueLabel.text!)! - race.cha)"
    chaLabel.text = actualChaLabel.text


func setTotalPointsWith(_ points: Int) 
    let currentValue = Int(totalPointValueLabel.text!)!
    totalPointValueLabel.text = "\(currentValue + points)"


这是我的UIPickerView 代码:

extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource 

func configurePickerView() 
    pickerView.delegate = self
    pickerView.dataSource = self
    pickerView.showsSelectionIndicator = true


func numberOfComponents(in pickerView: UIPickerView) -> Int 
    return 1


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

    if selectedTextField == 1 
        print(raceArray.count)
        return raceArray.count

     else 
        print(purchaseValues.count)
        return purchaseValues.count
    


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

    switch selectedTextField 
    case 1:
        raceTextField.text = raceArray[row].raceDesrciption
        return raceArray[row].raceDesrciption

    case 2:
        purchasedStrTextField.text = "\(purchaseValues[row].0)"
        return "\(purchaseValues[row].0)"

    case 3:
        purchasedDexTextField.text = "\(purchaseValues[row].0)"
        return "\(purchaseValues[row].0)"

    case 4:
        purchasedConTextField.text = "\(purchaseValues[row].0)"
        return "\(purchaseValues[row].0)"

    case 5:
        purchasedIntTextField.text = "\(purchaseValues[row].0)"
        return "\(purchaseValues[row].0)"

    case 6:
        purchasedWisTextField.text = "\(purchaseValues[row].0)"
        return "\(purchaseValues[row].0)"

    case 7:
        purchasedChaTextField.text = "\(purchaseValues[row].0)"
        return "\(purchaseValues[row].0)"

    default:
        print("no value")
    

    return nil


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 

    switch selectedTextField 
    case 1:
        setDefaultPoints(raceArray[row].raceDesrciption)

    case 2:
        actualStrLabel.text = "\(purchaseValues[row].0)"
        setTotalPointsWith(purchaseValues[row].1)

    case 3:
        actualDexLabel.text = "\(purchaseValues[row].0)"
        setTotalPointsWith(purchaseValues[row].1)

    case 4:
        actualConLabel.text = "\(purchaseValues[row].0)"
        setTotalPointsWith(purchaseValues[row].1)

    case 5:
        actualIntLabel.text = "\(purchaseValues[row].0)"
        setTotalPointsWith(purchaseValues[row].1)

    case 6:
        actualWisLabel.text = "\(purchaseValues[row].0)"
        setTotalPointsWith(purchaseValues[row].1)

    case 7:
        actualChaLabel.text = "\(purchaseValues[row].0)"
        setTotalPointsWith(purchaseValues[row].1)

    default:
        print("no value")
    


TextFields 配置:

extension ViewController: UITextFieldDelegate 

func congigureTextFields() 
    raceTextField.inputView = pickerView
    purchasedStrTextField.inputView = pickerView
    purchasedDexTextField.inputView = pickerView
    purchasedConTextField.inputView = pickerView
    purchasedIntTextField.inputView = pickerView
    purchasedWisTextField.inputView = pickerView
    purchasedChaTextField.inputView = pickerView

    raceTextField.delegate = self
    purchasedStrTextField.delegate = self
    purchasedDexTextField.delegate = self
    purchasedConTextField.delegate = self
    purchasedIntTextField.delegate = self
    purchasedWisTextField.delegate = self
    purchasedChaTextField.delegate = self
    raceModeStrTextField.delegate = self
    raceModeDexTextField.delegate = self
    raceModeConTextField.delegate = self
    raceModeIntTextField.delegate = self
    raceModeWisTextField.delegate = self
    raceModeChaTextField.delegate = self


func textFieldDidBeginEditing(_ textField: UITextField) 
    selectedTextField = textField.tag
    pickerView.reloadAllComponents()


还有purchaseValues数组:

let purchaseValues = [
(7, -4),
(8, -2),
(9, -1),
(10, 0),
(11, 1),
(12, 2),
(13, 3),
(14, 5),
(15, 7),
(16, 10),
(17, 13),
(18, 17)
]

【问题讨论】:

打印语句的输出无关紧要。您不能对调用titleForRow 的顺序做出任何假设。您对选择器视图的实际问题是什么?根据您的代码,选择器应该以正确的顺序显示元组中的键。 @rmaddy 我很抱歉控制台中的混乱!问题是选择器的 numberOfRowsInComponent 返回数组的计数 28 次。我不知道为什么会这样。 调用多少次或以什么顺序都无关紧要。你的选择器是否真的显示了正确的数据?不用担心您的打印报表。 我认为不是。因为那时我必须在滚动UIPickerView 时将键的值分配给特定的UILabel。最后它在UIPicker 的行中显示了正确的标题,但为UILabel 分配了不正确的值。 您能否发布一组适合的代码。您现在有两个 titleForRow 方法,新的一个引用您之前没有提到的变量。这是无法调试的。 【参考方案1】:

正如@rmaddy 所说:“...titleForRow 的唯一目的是告诉选取器视图为所请求的行使用什么标题。” - 这是我犯错的地方。非常感谢!

【讨论】:

以上是关于UIPickerView 在滚动时跳过数组中的值的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS Swift 3 中滚动到 UIPickerView 中的特定行

Swift 3:滚动视图中的 UIPickerView

如何限制 UIPickerView 组件

添加对象数组javascript的值

UITableViewCell 中的 UIPickerView - 如何选择值而不是滚动 TableView?

UITableView 使用 UIPIckerView 滚动到特定部分?