【问题标题】:Increase and Decrease values on Button click单击按钮时增加和减少值
【发布时间】:2018-07-18 07:50:59
【问题描述】:

我想在 UIlabel 上显示价值。当我按下 UIbutton 来增加或减少标签上的值时。这是我的代码,当我运行我的项目时,我的 uilabel 没有任何价值。

@IBAction func btnIncreaseAction(_ sender: Any) {


    var count = 0
    count = (count + 1)

   if let cell = (sender as? UIButton)?.superview?.superview?.superview as? ShoppingCell
    {
        //cell.lblForOnty.text = "\(cell.lblForOnty.text ?? 0 + 1)"
        cell.lblForOnty.text = String(count)
    }



}



  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! ShoppingCell

    var someValue: Int = 0 {
        didSet {
            cell.lblForOnty.text = "\(count)"
        }
    }

    return cell
}
}

【问题讨论】:

  • 你能在单元格中显示按钮层次结构吗
  • 只有你的单元格中的按钮增加了吗?

标签: ios swift uitableview uibutton


【解决方案1】:

这是应该在您的ViewController 中的代码。我假设 numberOfSections 是 1 并且在 numberOfRowsInSection 中你传递了你想要的行数。否则你需要修改这一行:let indexPath = IndexPath(row: sender.tag, section: 0).

var count = 0 // Count variable should be a global variable, you need it to decrease the value too.
@objc func increaseCounter(sender: UIButton) {
    //increase logic here
    count = (count + 1)
    let indexPath = IndexPath(row: sender.tag, section: 0)
    let cell = tableView.cellForRow(at: indexPath)
    cell.lblForOnty.text = "\(count)"
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! ShoppingCell

    // Add tag and action to your button
    cell.yourButton.tag = indexPath.row
    cell.yourButton.addTarget(self, action: #selector(increaseCounter(sender:)), for: .touchUpInside)

    return cell
}

【讨论】:

  • 好代码,但是有一个小问题假设我增加任何单元格值但是当我增加其他单元格值时,计数不是从 0 开始,而是从增加值开始。
  • 是的,在这种情况下,您必须管理每个单元格的值,这里是针对控制器的。 @akbarkhan
【解决方案2】:

superview?.superview?.superview 很奇怪。不要那样做。回调更可靠。

在子类ShoppingCell 中创建回调,IBActions 用于 in- 和 reduction 。将动作连接到按钮

class ShoppingCell: UITableViewCell {

    @IBOutlet weak var lblForOnty: UILabel!

    var callback : ((Int)->())?

    var counter = 0 {
      didSet {
         lblForOnty.text = "\(count)"
      }
    }

    @IBAction func btnIncreaseAction(_ sender: UIButton) {
        counter += 1
        callback?(counter)
    }

    @IBAction func btnDecreaseAction(_ sender: UIButton) {
        if counter > 0 { counter -= 1 }
        callback?(counter)
    }

}

cellForRow 中,将counter 值传递给单元格并使用回调来更新由dataSource 表示的模型,该模型是包含属性counter 的自定义结构或类。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID", for: indexPath) as! ShoppingCell

    let item = dataSource[indexPath.row]
    cell.counter = item.counter
    cell.callback = ( newValue in
        item.counter = newValue
    }

    return cell
}

超级视图和索引路径没有麻烦。

【讨论】:

    【解决方案3】:

    将变量移到函数之外 增加/减少函数内的计数并重新加载表,或者您也可以重新加载特定索引。 PFB 代码被剪断

    let indexPath = NSIndexPath(forRow: rowNumber, inSection: 0)
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation:   UITableViewRowAnimation.Top)
    

    【讨论】:

      【解决方案4】:

      请按以下步骤操作:

      第 1 步:

      创建对象类

      class QuantityBO : NSObject{
      var quantity      : Int?
      }
      

      第 2 步: 在您的班级中创建全局数组

      var arrQuantityList = [QuantityBO]()
      

      第三步:

      根据您的单元格数量分配值
      它可能会根据您的 api 反应而改变

      第四步:

      在 cellForRowAt 方法中请写:

      cell.lblQuantity.text = String(arrQuantityList[indexPath.row].quantity)
      cell.yourBtnName.tag = indexPath.row
      

      第 5 步:

      关于您的按钮操作

      arrQuantityList[sender.tag].quantity = arrQuantityList[sender.tag].quantity + 1
      let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
      tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation:   UITableViewRowAnimation.none)
      

      它可能对你有帮助。谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-16
        • 2018-01-12
        • 1970-01-01
        • 2019-08-03
        • 1970-01-01
        • 1970-01-01
        • 2018-05-23
        • 1970-01-01
        相关资源
        最近更新 更多