swift記帳 - text swift



Swift 3-UIButton從plist和數據庫中添加setTitle (1)

我有plist文件和數據庫,在plist文件中有英文字母,在數據庫中有row例如(倫敦),我把字符串轉換成字符數組並附加到plist文件中。 我通過(for循環)創建了14個UIButtons,並且我從數據庫的行中獲得了setTitle,並給予shuffle和從plist中添加字母並給定shuffle。

所以我的問題是,當從數據庫添加我的字符​​顯示我只是從數據庫中的字符,並像這樣洗牌:

但我不需要首先添加setTitle從數據庫中的所有字符和洗牌,其餘的空按鈕添加從plist文件中是這樣的:

我怎麼能像上面的圖片(第二張圖片)?

這是我的代碼,編寫代碼有什麼問題?

let fileName : String = " EnglishLetters"
let fileExt : String = "plist"
let pathRes = Bundle.main.path(forResource: fileName, ofType: fileExt)
let pathDict = NSDictionary(contentsOfFile: pathRes!)
var letters : [String] = pathDict?.object(forKey: "Letters") as! [String]

for data in listdata { // SQLite database

    let dataAnswer = data.ans // ans is a row in the SQLite database 
    let dataArrayAnswer = dataAnswer.characters.map{String($0)}
    letters.append(contentsOf: dataArrayAnswer)

    for char in dataArrayAnswer {}

        for i in 1...14 {

            let tileButton = UIButton(type: .roundedRect)
            let lettersAns = dataArrayAnswer.shuffled()[letters.distance(from: id, to: id)] // id is parameter
            tileButton.setTitle(lettersAns, for: .normal)
            tileButton.titleLabel?.font = UIFont(name: "HelveticaNeueW23forSKY-Bd", size: 15)
            tileButton.setTitleColor(.black, for: .normal)
            tileButton.setBackgroundImage(UIImage(named: "Cell"), for: .normal)            
            tileButton.addTarget(self, action: #selector(moveTile(sender:)), for: .touchUpInside)
            xAxis = xAxis + buttonWidth + space

            view.addSubview(tileButton)


            if i%7 == 0 {

               xAxis = emptySpace / 2
               yAxis = yAxis2 + space

            }

        }
    }
 }

你需要做的是首先創建一個數組與你所有的目標字母(洗牌和添加隨機字母),然後運行該數組更新按鈕標題。

首先用隨機字母填充一個14值的數組:

var presentedLetters : [String] = [String]()
var availableLetters : [String] = pathDict?.object(forKey: "Letters") as! [String]

var availableLetterIndexes : [Int] = Array(0..<availableLetters.count)

for letterAt in 0 ..< 14
{
    let randomIndex : Int = Int(arc4random_uniform(UInt32(availableLetterIndexes.count)))
    var charIndex : Int = availableLetterIndexes[randomIndex]

    presentedLetters.append(availableLetters[charIndex])

    availableLetterIndexes.remove(at: randomIndex)
}

為答案字符串生成可用位置:

var availableIndexes : [Int] = Array(0..<presentedLetters.count)

然後從數據庫中查看你的信件並將它們添加到目標數組中的隨機位置(同時記住添加的索引以防止覆蓋字母):

var addedAnswerIndexes : [Int] = [Int]()

let answerString = data.ans // ans is a row in the SQLite database 
let answerCharacters = answerString.characters.map{String($0)}

for answerCharAt in answerCharacters
{
    let randomIndex : Int = Int(arc4random_uniform(UInt32(availableIndexes.count)))
    var charIndex : Int = availableIndexes[randomIndex]

    presentedLetters[charIndex] = answerCharAt

    availableIndexes.remove(at: randomIndex)
}

然後呈現的信件將有一個所有相關值的數組。

編輯:

要將標題添加到按鈕,只需重新顯示“提示”數組:

        for i in 1...14 {

            let tileButton = UIButton(type: .roundedRect)
            tileButton.setTitle(presentedLetters[i-1], for: .normal)

            ......

        }
    }

祝你好運!





plist