在单元格上单击,标签的值应增加和减少

时间:2019-03-29 07:01:24

标签: arrays swift

我有一个数组,首先将其加载到tableview单元格中,然后单击该单元格,该值将附加到数组中,并将数组中所有元素的总和打印到UILabel。

当数组具有不同的元素时,此方法工作正常,但在具有相同的元素时,它会失败。

这是我的代码。

let amount:[String] = ["1","1","1"]
var amountsSelected:[String] = []

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       // Check if it is not in the array
    if(!amountsSelected.contains(amount[indexPath.row])){

        amountsSelected.append(amount[indexPath.row])

        let intArray = amountsSelected.map { Int($0)!}

        let total = intArray.reduce(0, +)
        moneyText.text = String(total)

        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark
        }


    }else if(amountsSelected.contains(amount[indexPath.row])){
       //check if it is present in the array
        amountsSelected.append(amount[indexPath.row])

        let intArray = amountsSelected.map { Int($0)!}

        let total = intArray.reduce(0, +)
        moneyText.text = String(total)

        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark
        }


    }else{
       //just remove
        amountsSelected = amountsSelected.filter({$0 != amount[indexPath.row]})
        print(amountsSelected)
        let intArray = amountsSelected.map { Int($0)!}

        let total = intArray.reduce(0, +)
        moneyText.text = String(total)

        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .none
        }
    }

}

即使取消选择UILabel的值,尝试此操作也会保持增加而不是减少。 任何帮助将不胜感激

3 个答案:

答案 0 :(得分:1)

我同意Gerriet的观点:“ if”语句或“ else if”语句之一必须为真,因为它们彼此相反。因此,您的“其他”声明将永远不会触发。

如果(不在数组中)则执行操作;其他 如果(在数组中)然后做同样的事情...

答案 1 :(得分:1)

在现有代码中,仅当该数组已不包含相同字符串时,才将值添加到该数组。因此,当您尝试使用相同的元素["1","1","1"]时,这些元素不会被多次添加,因此会导致错误的结果且行为不符合您的预期。

要解决此问题,您可以定义一个数组selectedIndexPath,其中包含已经选择的indexPaths

var selectedIndexPath: [IndexPath] = []

更新您的didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if selectedIndexPath.contains(indexPath) {
        selectedIndexPath.removeAll { (indPath) -> Bool in
            indexPath == indPath
        }
        let selectedValue = amount[indexPath.row]
        let deselectedIndex = amountsSelected.firstIndex(of: selectedValue)
        amountsSelected.remove(at: deselectedIndex!)

        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .none
        }
    } else {
        selectedIndexPath.append(indexPath)

        let selectedValue = amount[indexPath.row]
        amountsSelected.append(selectedValue)

        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark
        }
    }
    let intArray = amountsSelected.map { Int($0)!}
            let total = intArray.reduce(0, +)
            moneyText.text = String(total)
            print(total)
}

并修改您的cellForRow方法以显示checkMark如果数组中包含indexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // YOUR EXISTING CODE HERE dequeueReusableCell
        //let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        //cell?.textLabel?.text = amount[indexPath.row]

        //THIS CHECK NEEDS TO BE ADDED ON CELL FOR ROW AT METHOD
        if selectedIndexPath.contains(indexPath) {
            cell?.accessoryType = .checkmark
        } else {
            cell?.accessoryType = .none
        }

        return cell!
}

希望有帮助

答案 2 :(得分:1)

我遇到了问题,您必须创建一个 selectedIndexes 数组,并在didSelect委托中检查该索引是否存在于数组中(如果存在),然后将其从 selectedIndexes 中删除数组,否则将其添加到该文件中。

此代码将解决您的问题:

let amount:[String] = ["1","1","1"]
var selectedIndexes = [Int]()


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if(selectedIndexes.contains(indexPath.row)){
        selectedIndexes = selectedIndexes.filter{ $0 != indexPath.row }
        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .none
        }
    } else {
        selectedIndexes.append(indexPath.row)
        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark
        }
    }

    // update total count label here

    let intArray = selectedIndexes.compactMap { Int(amount[$0]) }

    let total = intArray.reduce(0, +)
    moneyText.text = String(total)

}

希望它会对您有所帮助。