Swift 更新标签时Pickerview延迟

Swift 更新标签时Pickerview延迟,swift,uipickerview,Swift,Uipickerview,我正在使用选择器视图,希望更新屏幕上与当前所选项目相关的内容 以下是程序中的代码: func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { TeamLabel.text = teams [row] attackField.text = "\(attacks [row])" defenseField.te

我正在使用选择器视图,希望更新屏幕上与当前所选项目相关的内容

以下是程序中的代码:

   func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    TeamLabel.text = teams [row]
    attackField.text = "\(attacks [row])"
    defenseField.text = "\(defenses [row])"
    currentRow = row
    return teams [row]
}
这会在屏幕上显示球队名称、进攻和防守值

然而,当滚动选择器时,我经常得到错误的结果。文本输出与选择器中当前的内容不匹配。你知道我该怎么解决这个问题吗


谢谢。

您使用了错误的委托方法来更新文本字段(或标签)

将标题福罗更新为:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return teams[row]
}
然后执行
didSelectRow
delegate方法更新字段:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    TeamLabel.text = teams[row]
    attackField.text = "\(attacks[row])"
    defenseField.text = "\(defenses[row])"
}

您使用了错误的委托方法来更新文本字段(或标签)

将标题福罗更新为:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return teams[row]
}
然后执行
didSelectRow
delegate方法更新字段:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    TeamLabel.text = teams[row]
    attackField.text = "\(attacks[row])"
    defenseField.text = "\(defenses[row])"
}

为什么要更新
titleForRow
picker-view-delegate方法中的其他UI元素?这个方法应该做一件事,而且只能做一件事-返回一个string.OK。但是我如何才能根据当前选择的内容更新内容呢?当然可以,但您可以使用正确的委托方法来进行更新:
pickerView(\upickerview:UIPickerView,didSelectRow-row-Int,incomonent-component-Int)
OMgosh。我刚刚意识到。它应该在第二行,而不是标题行。谢谢。为什么要更新
titleForRow
picker-view-delegate方法中的其他UI元素?这个方法应该做一件事,而且只能做一件事-返回一个string.OK。但是我如何才能根据当前选择的内容更新内容呢?当然可以,但您可以使用正确的委托方法来进行更新:
pickerView(\upickerview:UIPickerView,didSelectRow-row-Int,incomonent-component-Int)
OMgosh。我刚刚意识到。它应该在第二行,而不是标题行。谢谢,是的。非常感谢。非常感谢。