簡體   English   中英

PickerVIew.selectRow不起作用

[英]PickerVIew.selectRow doesn't work

我正在用Swift 3和Xcode 8.3.3編寫iOS應用。 我的應用程序是一台老虎機。 在我的autoWinBtn函數中,當我想要選擇特定的行(帶有selectRow(_ row:Int,inComponent component:Int,animation:Bool) )時,它行不通…

這是“打印”的結果

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
}


}

我做了很多研究,但沒有結果與我合作

將您的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
    }
}

您還應該在estAutoWin = true行之后添加break語句,因為找到匹配的水果時不必繼續循環。

estAutoWin = true
break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM