【问题标题】:On cell click the value of a label should increase and decrease在单元格上单击标签的值应该增加和减少
【发布时间】:2019-03-29 07:01:23
【问题描述】:

我有一个数组,我首先将它加载到 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 的值后尝试这个,它也会不断增加而不是减少。 任何帮助将不胜感激

【问题讨论】:

  • 请指出 if 和 else-if 子句的区别。也许我错过了一些东西,但对我来说它们看起来一样。另外,你有没有设法进入最后一个 else 子句,对我来说似乎是不可能的。
  • 首先,如果我比较 if(!amountsSelected.contains(amount[indexPath.row])) //即,我检查它是否不存在于数组中。在 elseif 我做 else if(amountsSelected.contains(amount[indexPath.row])) //检查它们是否相同。在 else 我做 amountSelected = amountSelected.filter({$0 != amount[indexPath.row]} ) 只是删除
  • 好的,我们来解决一些问题:首先,if 和 else-if-clause 中的代码是相同的。因此,无论条件如何,都会发生同样的事情。其次,不能调用 else 子句(正如 Daniel 指出的那样)。第三,您刚刚发布了 didSelect 方法,因此取消选择它时不会发生任何事情(因为您似乎预计会减少)。

标签: arrays swift


【解决方案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】:

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

    if (不在数组中) then do stuff;别的 如果(在数组中)然后做同样的事情......

    【讨论】:

      【解决方案3】:

      我遇到了您的问题,您必须创建一个 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)
      
      }
      

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-22
        • 2018-01-12
        • 2021-09-16
        • 1970-01-01
        • 1970-01-01
        • 2020-04-22
        • 1970-01-01
        相关资源
        最近更新 更多