ios - 加号或减号按钮值不会快速更新
问题描述
我在购物车视图控制器上工作,我尝试了一些代码,如下图所示。
值更新tableview中的每一行,如果我点击product1 plusbutton计数增加显示1。当我点击product2 plusbutton值显示2.count正在增加.minus每次减号也一样工作。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell.minusButton.tag = indexPath.row
cell.plusbutton.tag = indexPath.row
cell.minusButton.addTarget(self, action: #selector(minusbuttonClick), for: .touchUpInside)
cell.plusbutton.addTarget(self, action: #selector(plusButtonClick), for: .touchUpInside)
return cell
}
@objc func minusbuttonClick(sender : UIButton)
{
let cell = Tableview.cellForRow(at: NSIndexPath(row: sender.tag, section: 0) as IndexPath) as! CustomTableViewCell
if(count > 0){
count -= 1
}
let myString = String(count)
cell.countLabel.text = myString
if count == 0{
cell.countLabel.text = ""
}
self.Tableview.reloadData()
}
@objc func plusButtonClick(sender : UIButton)
{
let cell = Tableview.cellForRow(at: NSIndexPath(row: sender.tag, section: 0) as IndexPath) as! CustomTableViewCell
count += 1
let myString = String(count)
cell.countLabel.text = myString
self.Tableview.reloadData()
}
我必须显示当我点击 product1 值应该是 1,如果我点击 product2 值作为 1 减也同样减少
解决方案
首先要做的是创建一个结构来保存您的数据
struct ProductData {
var count : Int
var name : String
}
你可以在你的 ViewController 中使用它
var productData : [ProductData] = []
当然,您需要添加一些数据 - 这是一个简单的开始设置
override func viewDidLoad() {
super.viewDidLoad()
// set up some data.
productData.append(ProductData(count: 0, name: "product 1"))
productData.append(ProductData(count: 0, name: "product 2"))
productData.append(ProductData(count: 0, name: "product 3"))
productData.append(ProductData(count: 0, name: "product 4"))
}
使用前面描述的委托模型swift:如何在点击单元格中的按钮时获取 indexpath.row?更新您的自定义表格视图单元格
protocol TableViewCellCustomDelegate: class {
func buttonTapped(index : Int, delta : Int)
}
class TableViewCellCustom: UITableViewCell {
weak var delegate: TableViewCellCustomDelegate?
@IBOutlet weak var minusButton: UIButton!
@IBOutlet weak var plusButton: UIButton!
@IBOutlet weak var countLabel: UILabel!
@IBOutlet weak var productLabel: UILabel!
override func prepareForReuse() {
super.prepareForReuse()
self.delegate = nil
}
@IBAction func minusButtonClick(_ sender: UIButton) {
delegate?.buttonTapped(index: sender.tag, delta : -1)
}
@IBAction func plusButtonClick(_ sender: UIButton) {
delegate?.buttonTapped(index: sender.tag,delta : +1)
}
}
更新视图控制器中的委托方法。我在这里用一种方法来添加或删除它,但如果你还想做其他事情,你可以把它分开。
extension ViewController: TableViewCellCustomDelegate {
func buttonTapped(index: Int, delta : Int)
{
productData[index].count += delta
if productData[index].count < 0
{
productData[index].count = 0
}
tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
}
}
并更新您的cellForRowAt
定义
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! TableViewCellCustom
cell.countLabel.text = "\(productData[indexPath.row].count)"
cell.productLabel.text = productData[indexPath.row].name
cell.minusButton.tag = indexPath.row
cell.plusButton.tag = indexPath.row
cell.delegate = self
return cell
}
推荐阅读
- javascript - 我正在尝试从 ES 类参数创建一个数组,但我得到一个空数组,为什么?
- kubernetes - Kubernetes 和故障转移服务
- r - 如何在 download_html 和 download.file 中使用 UTF-8 文件夹名称
- python - 努力让 Django 服务器在虚拟环境中运行
- ios - 使用用户 iOS 货币设置
- tensorflow - Tensorflow,如何实现排序层
- assembly - Shellcode:非法指令
- php - 不存在的 Codeigniter 不起作用
- python - 在函数内部调用 ThreadPoolExecutor 时不使用多个工作程序
- python - 使用 subprocess 模块运行 .bat 文件并将其传递给输入