Swift 3-UIButton从plist和数据库中添加setTitle [英] Swift 3 - UIButton adding setTitle from plist and database

查看:135
本文介绍了Swift 3-UIButton从plist和数据库中添加setTitle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有plist文件和数据库,在plist文件中有英文字母,在数据库行中有例如String(伦敦),我将String转换为字符数组并附加到plist文件中.我通过(for循环)创建了14个UIButton,并从数据库行中获取了setTitle,并进行了混洗,并从plist中添加了字母,并进行了混洗.

I have plist file and database, in the plist file has English letters and in database row has String for example (London) and I convert String to characters array and appended into plist file. I created 14 UIButtons by (for loop) and I was given setTitle from row of database and given shuffle and adding letters from plist and given 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

            }

        }
    }
 }

推荐答案

您需要做的是首先创建一个包含所有目标字母(随机和添加的随机字母)的数组,然后在该数组上运行以更新按钮标题

What you need to do is first create an array with all your target letters (shuffled and added random letters) and then run over that array to update the button titles.

首先用随机字母填充14值数组:

Start by filling a 14 value array with random letters:

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)
}

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

Generate available positions for the answer string:

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

然后遍历数据库中的字母并将它们添加到目标数组中的随机位置(同时记住添加的索引以防止覆盖字母):

Then go over your letters from the db and add them in random places inside the target array (while remembering added indexes to prevent overriding letters):

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)
}

然后,presentedLetters将具有所有相关值的数组.

And then presentedLetters will have an array of all relevant values.

要将标题添加到按钮,只需遍历presentedLetters数组:

To add the titles to the buttons, just go over presentedLetters array:

        for i in 1...14 {

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

            ......

        }
    }

祝你好运!

这篇关于Swift 3-UIButton从plist和数据库中添加setTitle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆