【发布时间】:2020-02-24 16:51:35
【问题描述】:
创建一个简单的货币兑换应用程序,用户可以在其中从列表中选择一种货币,然后从调用的同一个列表中选择另一种货币,然后重定向到比较货币的新视图。在第一次选择之后,我禁用了选择选项,以便无法再次选择它。尽管它正确地禁用了我想要的值,但它也随机禁用了另一个我不知道为什么的 x 量。不知道我编码错了什么,所以任何帮助表示赞赏。执行禁用的函数在cellForRowAt
class SelectCurrencyTableViewController: UITableViewController {
var selectedCurrency: String?
var codesToConvert = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
// Get the JSON data to insert into the table
func parseJSONData()-> Array<Any> {
var finalArray = [Any]()
if let url = Bundle.main.url(forResource: "currencies", withExtension: "json") {
do {
let data = try Data(contentsOf: url)
let jsonResult = try JSONSerialization.jsonObject(with: data)
if var jsonArray = jsonResult as? [String] {
while jsonArray.count > 0 {
let result: [String] = Array(jsonArray.prefix(2))
finalArray.append(result)
jsonArray.removeFirst(2)
}
}
} catch {
print(error)
}
}
return finalArray
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return parseJSONData().count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCellController
if let array = parseJSONData()[indexPath.row] as? [String]{
cell.countryCodeLabel.text = array[0]
cell.currencyLabel.text = array[1]
cell.countryFlag.image = UIImage(named: array[0])
// if selected is the same as the current one, disable that row
if (selectedCurrency == array[0]){
cell.isUserInteractionEnabled = false
cell.countryCodeLabel.alpha = 0.3
cell.currencyLabel.alpha = 0.3
cell.countryFlag.alpha = 0.3
}
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (selectedCurrency != nil){
if let array = parseJSONData()[indexPath.row] as? [String] {
codesToConvert.append(selectedCurrency!)
codesToConvert.append(array[0])
}
self.performSegue(withIdentifier: "viewConversions", sender: self)
} else {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "tableView") as! SelectCurrencyTableViewController
vc.modalPresentationStyle = .overCurrentContext
if let array = parseJSONData()[indexPath.row] as? [String]{
vc.selectedCurrency = array[0]
show(vc, sender: self)
}
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60.0
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "viewConversions" {
let VCTV = segue.destination as! ViewConversionsTableViewController
VCTV.codesToConvert = codesToConvert
}
}
}
【问题讨论】:
标签: arrays swift uitableview