Swift 3 - UIButton adding setTitle from plist and database(Swift 3 - UIButton 从 plist 和数据库添加 setTitle)
问题描述
我有 plist 文件和数据库,在 plist 文件中有英文字母,在数据库行中有字符串,例如(伦敦),我将字符串转换为字符数组并附加到 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)
}
然后 presentLetters 将有一个包含所有相关值的数组.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swift 3 - UIButton 从 plist 和数据库添加 setTitle
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01