PickerVIew.selectRow不起作用

时间:2017-08-19 09:31:57

标签: ios swift3 uipickerview

我正在编写一个带有swift 3和Xcode 8.3.3的iOS应用程序。我的应用是老虎机。 在我的 autoWinBtn 函数中,当我想要选择一个特定的行(使用 selectRow(_ row:Int,inComponent component:Int,animated:Bool))时,它不起作用...

以下是«print»

的结果
component1 : [0, 3, 5, 0, 7, 3, 8, 8, 6, 5, 2]
component2 : [6, 3, 1, 8, 5, 7, 3, 1, 7, 7, 0]
component3 : [6, 5, 6, 2, 5, 7, 0, 5, 7, 7, 8]
i : 0 -- 3 / 10 / 6 // there are 0 in position 3 for component1
                    //                         10 for component2
                     //                        6 for component3
pos pickerView 1 : 3
pos pickerView 2 : 1
pos pickerView 3 : 6

pickerView 2应位于10位置

这是我的所有代码:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var lasVegasCasinoLabel: UILabel!
@IBOutlet weak var casinoRouletttePickerView: UIPickerView!

@IBOutlet weak var winLoseLabel: UILabel!

@IBOutlet weak var moneyLabel: UILabel!

var collectionFruit: [String] = [String]()
var collectionMoney: [Int] = [Int]()
var component1: [Int] = [Int]()
var component2: [Int] = [Int]()
var component3: [Int] = [Int]()

var money: Int = 0


@IBAction func autoWinBtn(_ sender: UIButton) {
    var estAutoWin: Bool = false
    print("component1 : \(component1)")
    print("component2 : \(component2)")
    print("component3 : \(component3)")

    for i in 0...collectionFruit.count-1 {
        let fruit1 = isPresent(component: component1, number: i)
        let fruit2 = isPresent(component: component2, number: i)
        let fruit3 = isPresent(component: component3, number: i)

        if (fruit1 != -1 && fruit2 != -1 && fruit3 != -1  && estAutoWin == false){

            print("i : \(i) -- \(fruit1) / \(fruit2) / \(fruit3)")

            casinoRouletttePickerView.selectRow(fruit1, inComponent: 0, animated: true)
            casinoRouletttePickerView.selectRow(fruit2, inComponent: 1, animated: true)
            casinoRouletttePickerView.selectRow(fruit3, inComponent: 2, animated: true)

            print("pos pickerView 1 : \(casinoRouletttePickerView.selectedRow(inComponent: 0))")
            print("pos pickerView 2 : \(casinoRouletttePickerView.selectedRow(inComponent: 1))")
            print("pos pickerView 3 : \(casinoRouletttePickerView.selectedRow(inComponent: 2))")

            estAutoWin = true
        }
    }
    winLose()

}

func isPresent(component: [Int], number: Int) -> Int {
    var present: Int = -1
    for i in 0...component.count-1 {
        if component[i] == number {
            present = i
        }
    }
    return present
}

@IBAction func playButtonAction(_ sender: UIButton) {
    money -= 10
    casinoRouletttePickerView.selectRow(randomNumber(num: collectionFruit.count-1), inComponent: 0, animated: true)
    casinoRouletttePickerView.selectRow(randomNumber(num: collectionFruit.count-1), inComponent: 1, animated: true)
    casinoRouletttePickerView.selectRow(randomNumber(num: collectionFruit.count-1), inComponent: 2, animated: true)

    winLose()
}

func winLose(){
    if (component1[casinoRouletttePickerView.selectedRow(inComponent: 0)] ==
        component2[casinoRouletttePickerView.selectedRow(inComponent: 1)] &&
        component1[casinoRouletttePickerView.selectedRow(inComponent: 0)] ==
        component3[casinoRouletttePickerView.selectedRow(inComponent: 2)]){

        winLoseLabel.text = "Gagner"
        winLoseLabel.textColor = UIColor.green

        money += 20
        moneyLabel.text = "\(money)"
    } else {
        winLoseLabel.text = "Perdu"
        winLoseLabel.textColor = UIColor.red
        moneyLabel.text = "\(money)"
    }
}
func randomNumber(num: Int) -> Int{
        return Int(arc4random_uniform(UInt32(num)))
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    money = 100
    collectionFruit = ["", "", "", "", "", "", "", "", ""]
    collectionMoney = [1, 3, 5, 10, 20, 50, 100, 200, 500]

    for _ in 0...10 {
        component1.append(randomNumber(num: collectionFruit.count))
        component2.append(randomNumber(num: collectionFruit.count))
        component3.append(randomNumber(num: collectionFruit.count))
    }

    lasVegasCasinoLabel.text = "Las Vegas \n Casino"

    casinoRouletttePickerView.delegate = self
    casinoRouletttePickerView.dataSource = self
}


// returns the number of 'columns' to display.
@available(iOS 2.0, *)
public func numberOfComponents(in pickerView: UIPickerView) -> Int{
    return 3
}


// returns the # of rows in each component..
@available(iOS 2.0, *)
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
    return collectionFruit.count
}

func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    return 100
}

func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
    return 100
}
@available(iOS 2.0, *)
public func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

    let pickerLabel: UILabel = UILabel()
    switch component {
    case 0:
        pickerLabel.text = collectionFruit[component1[row]]

    case 1:
        pickerLabel.text = collectionFruit[component2[row]]

    case 2:
        pickerLabel.text = collectionFruit[component3[row]]

    default:
        print("Erreur de component")
    }

    pickerLabel.font = UIFont(name: "Apple Color emoji", size: 100)
    pickerLabel.textAlignment = NSTextAlignment.center
    return pickerLabel
}


}

我做了很多研究,但没有一个结果与我合作

1 个答案:

答案 0 :(得分:0)

将您的numberOfRowsInComponent数据源方法更改为以下内容:

// returns the # of rows in each component..
@available(iOS 2.0, *)
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
    switch component {
    case 0: return component1.count
    case 1: return component2.count
    case 2: return component3.count
    default: return 0
    }
}

您还应该在break行之后添加estAutoWin = true语句,因为当您找到匹配的水果时不必继续循环播放。

estAutoWin = true
break