Checkmark在同一地点的所有23个国家/地区被接受。但是我怎样才能在一个地方设置它

时间:2016-07-26 18:36:32

标签: ios swift uitableview checkmark

enter image description here enter image description here我有两个UITableView:一个是国家,另一个是硬币。当我选择一个国家时,我可以看到该国家的硬币数量。当我点击硬币数量时,勾选一个复选标记。但是当点击一个国家的硬币项目时,勾选标记是所有23个国家。

如何切换到仅检查被点击的国家/地区?

//第一个TableView(国家/地区)

import UIKit

class MainUITableViewController: UITableViewController {

var flagName = [
    "Andorra",
    "Austria",
    "Belgium",
    "Cyprus",
    "Estonia",
    "Finland",
    "France",
    "Germany",
    "Greece",
    "Ireland",
    "Italy",
    "Latvia",
    "Lithuania",
    "Luxembourg",
    "Malta",
    "Monaco",
    "Netherlands",
    "Portugal",
    "San_Marino",
    "Slovakia",
    "Slovenia",
    "Spain",
    "Vatican_City"]

var flagImage = [
    "Andorra",
    "Austria",
    "Belgium",
    "Cyprus",
    "Estonia",
    "Finland",
    "France",
    "Germany",
    "Greece",
    "Ireland",
    "Italy",
    "Latvia",
    "Lithuania",
    "Luxembourg",
    "Malta",
    "Monaco",
    "Netherlands",
    "Portugal",
    "San_Marino",
    "Slovakia",
    "Slovenia",
    "Spain",
    "Vatican_City"]

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.destinationViewController is SecondUITableViewController {

        let second_vc = segue.destinationViewController as! SecondUITableViewController
        second_vc.current_index = (self.tableView.indexPathForSelectedRow?.row)!

    }

}

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return flagName.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ComponentsUITableViewCell

    cell.flagImageView?.image = UIImage(named: flagImage[indexPath.row])
    cell.nameInCell?.text = flagName[indexPath.row]

    return cell
}

}

//第二个TableView(硬币)

import UIKit

class SecondUITableViewController: UITableViewController {

var coinsNames = ["1 euro cent", "2 euro cent", "5 euro cent", "10 euro cent", "20 euro cent", "50 euro cent", "1 euro", "2 euro"]
var coinsImageA = ["Andorra_1", "Andorra_2", "Andorra_5", "Andorra_10", "Andorra_20", "Andorra_50", "Andorra_1e", "Andorra_2e"]
var coinsImageB = ["Austria_1", "Austria_2", "Austria_5", "Austria_10", "Austria_20", "Austria_50", "Austria_1e", "Austria_2e"]
var coinsImageC = ["Belgium_1", "Belgium_2", "Belgium_5", "Belgium_10", "Belgium_20", "Belgium_50", "Belgium_1e", "Belgium_2e"]
var coinsImageD = ["Cyprus_1", "Cyprus_2", "Cyprus_5", "Cyprus_10", "Cyprus_20", "Cyprus_50", "Cyprus_1e", "Cyprus_2e"]
var coinsImageE = ["Estonia_1", "Estonia_2", "Estonia_5", "Estonia_10", "Estonia_20", "Estonia_50", "Estonia_1e", "Estonia_2e"]
var coinsImageF = ["Finland_1", "Finland_2", "Finland_5", "Finland_10", "Finland_20", "Finland_50", "Finland_1e", "Finland_2e"]
var coinsImageG = ["France_1", "France_2", "France_5", "France_10", "France_20", "France_50", "France_1e", "France_2e"]
var coinsImageH = ["Germany_1", "Germany_2", "Germany_5", "Germany_10", "Germany_20", "Germany_50", "Germany_1e", "Germany_2e"]
var coinsImageI = ["Greece_1", "Greece_2", "Greece_5", "Greece_10", "Greece_20", "Greece_50", "Greece_1e", "Greece_2e"]
var coinsImageJ = ["Ireland_1", "Ireland_2", "Ireland_5", "Ireland_10", "Ireland_20", "Ireland_50", "Ireland_1e", "Ireland_2e"]
var coinsImageK = ["Italy_1", "Italy_2", "Italy_5", "Italy_10", "Italy_20", "Italy_50", "Italy_1e", "Italy_2e"]
var coinsImageL = ["Latvia_1", "Latvia_2", "Latvia_5", "Latvia_10", "Latvia_20", "Latvia_50", "Latvia_1e", "Latvia_2e"]
var coinsImageM = ["Lithuania_1", "Lithuania_2", "Lithuania_5", "Lithuania_10", "Lithuania_20", "Lithuania_50", "Lithuania_1e", "Lithuania_2e"]
var coinsImageN = ["Luxembourg_1", "Luxembourg_2", "Luxembourg_5", "Luxembourg_10", "Luxembourg_20", "Luxembourg_50", "Luxembourg_1e", "Luxembourg_2e"]
var coinsImageO = ["Malta_1", "Malta_2", "Malta_5", "Malta_10", "Malta_20", "Malta_50", "Malta_1e", "Malta_2e"]
var coinsImageP = ["Monaco_1", "Monaco_2", "Monaco_5", "Monaco_10", "Monaco_20", "Monaco_50", "Monaco_1e", "Monaco_2e"]
var coinsImageQ = ["Netherlands_1", "Netherlands_2", "Netherlands_5", "Netherlands_10", "Netherlands_20", "Netherlands_50", "Netherlands_1e", "Netherlands_2e"]
var coinsImageR = ["Portugal_1", "Portugal_2", "Portugal_5", "Portugal_10", "Portugal_20", "Portugal_50", "Portugal_1e", "Portugal_2e"]
var coinsImageS = ["San_Marino_1", "San_Marino_2", "San_Marino_5", "San_Marino_10", "San_Marino_20", "San_Marino_50", "San_Marino_1e", "San_Marino_2e"]
var coinsImageT = ["Slovakia_1", "Slovakia_2", "Slovakia_5", "Slovakia_10", "Slovakia_20", "Slovakia_50", "Slovakia_1e", "Slovakia_2e"]
var coinsImageU = ["Slovenia_1", "Slovenia_2", "Slovenia_5", "Slovenia_10", "Slovenia_20", "Slovenia_50", "Slovenia_1e", "Slovenia_2e"]
var coinsImageV = ["Spain_1", "Spain_2", "Spain_5", "Spain_10", "Spain_20", "Spain_50", "Spain_1e", "Spain_2e"]
var coinsImageW = ["Vatican_City_1", "Vatican_City_2", "Vatican_City_5", "Vatican_City_10", "Vatican_City_20", "Vatican_City_50", "Vatican_City_1e", "Vatican_City_2e"]

var current_index = 0

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return coinsNames.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell2 = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! Components2UITableViewCell

    cell2.tintColor = UIColor.whiteColor()

    switch current_index {

    case 0:
        cell2.coinsImageView?.image = UIImage(named: coinsImageA[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 1:
        cell2.coinsImageView?.image = UIImage(named: coinsImageB[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 2:
        cell2.coinsImageView?.image = UIImage(named: coinsImageC[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 3:
        cell2.coinsImageView?.image = UIImage(named: coinsImageD[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 4:
        cell2.coinsImageView?.image = UIImage(named: coinsImageE[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 5:
        cell2.coinsImageView?.image = UIImage(named: coinsImageF[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 6:
        cell2.coinsImageView?.image = UIImage(named: coinsImageG[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 7:
        cell2.coinsImageView?.image = UIImage(named: coinsImageH[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 8:
        cell2.coinsImageView?.image = UIImage(named: coinsImageI[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 9:
        cell2.coinsImageView?.image = UIImage(named: coinsImageJ[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 10:
        cell2.coinsImageView?.image = UIImage(named: coinsImageK[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 11:
        cell2.coinsImageView?.image = UIImage(named: coinsImageL[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 12:
        cell2.coinsImageView?.image = UIImage(named: coinsImageM[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 13:
        cell2.coinsImageView?.image = UIImage(named: coinsImageN[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 14:
        cell2.coinsImageView?.image = UIImage(named: coinsImageO[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 15:
        cell2.coinsImageView?.image = UIImage(named: coinsImageP[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 16:
        cell2.coinsImageView?.image = UIImage(named: coinsImageQ[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 17:
        cell2.coinsImageView?.image = UIImage(named: coinsImageR[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 18:
        cell2.coinsImageView?.image = UIImage(named: coinsImageS[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 19:
        cell2.coinsImageView?.image = UIImage(named: coinsImageT[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 20:
        cell2.coinsImageView?.image = UIImage(named: coinsImageU[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 21:
        cell2.coinsImageView?.image = UIImage(named: coinsImageV[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]
    case 22:
        cell2.coinsImageView?.image = UIImage(named: coinsImageW[indexPath.row])
        cell2.cionsName?.text = coinsNames[indexPath.row]

        default: cell2

    }

    cell2.accessoryType = NSUserDefaults.standardUserDefaults().boolForKey(coinsNames[indexPath.row]) == true ? .Checkmark : .None
    return cell2
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //        self.coinsNames[indexPath.row]
    NSUserDefaults.standardUserDefaults().setBool(!NSUserDefaults.standardUserDefaults().boolForKey(coinsNames[indexPath.row]), forKey: coinsNames[indexPath.row])
    NSUserDefaults.standardUserDefaults().synchronize()
    self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
}

}

2 个答案:

答案 0 :(得分:0)

我可以建议你做一个这样的元组:

typealias yourItem = (flagName: String, flagImage: String, selected: Bool)

然后将tableView dataSource 更改为[yourItem]数组

let tableData: [yourItem] = [yourItem(), yourItem()....]

最后在 cellForRowAtIndexPath 中,您应该检查所选的状态

let item = tableData[indexPath.row]
cell.accessoryView = item.selected ? .Checkmark : nil

答案 1 :(得分:0)

您必须使用true初始化NSUserDefaults布尔值,首先呈现UITableViewcellForRowAtIndexPath)以将所有附件设置为.Checkmark

cell2.accessoryType = NSUserDefaults.standardUserDefaults().boolForKey(coinsNames[indexPath.row]) == true ? .Checkmark : .None

检查您首次设置NSUserDefaults布尔值的位置,以验证它们是否已使用true进行初始化。

如果没有,请继续检查整个应用程序中的所有NSUserDefaults作业。

你应该能够找到这个程序的问题所在,一旦你切换附件的唯一地方 - 按照片段 - 就在前面一行。

在应用程序中提供所有NSUserDefaults作业将有助于我们提出更准确的答案。

在使用它之前,您似乎没有初始化NSUserDefaults bool,尝试遍历coinsNames数组并在每个键上将NSUserDefault值设置为false:

for coinName in coinsNames {
    NSUserDefaults.standardUserDefaults().setBool(false, coinName)
}