0

使用 Swiggy 之类的应用程序。

  1. 在 UITableview 中显示商品和价格。
  2. 单击添加按钮时要增加的项目数量和数量,单击减号按钮时要减少的数量和数量。
  3. 增加和减少应该分别加载到每个单元格中

这是我的代码:

var vegListPrice = [130,
                        200,
                        345,
                        456,
                        120,
                        110,
                        345,
                        300,
                        160,
                        210]

var vegListName = ["Bhindi masala veggies recipes",
                       "Aloo matar veggie recipes",
                       "Dum aloo veggie recipes",
                       "Veg kurma veggie recipes",
                       "Lauki kofta veggie recipesss",
                       "Aloo gobi masala",
                       "Baingan bharta veggie recipes",
                       "Bharwan shimla mirch veggie recipes",
                       "Vegetable kadai veggie recipes.jpg",
                       "Aloo palak palak recipes"]

extension ViewController:UITableViewDataSource,UITableViewDelegate{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.vegListName.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:ItmCell = self.veglistTbl.dequeueReusableCell(withIdentifier: "ItmCell") as! ItmCell
        cell.nameLbl.text = vegListName[indexPath.row]
        cell.addLbl.text = "$" + String(vegListPrice[indexPath.row])
        cell.increaseBtn.tag = indexPath.row
        cell.increaseBtn.addTarget(self, action:#selector(IncreasePressed(_:)), for:.touchUpInside)
        cell.decreaseBtn.tag = indexPath.row
        cell.decreaseBtn.addTarget(self, action:#selector(DecreasePressed(_:)), for:.touchUpInside)

        return cell

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 180
    }

    @objc func IncreasePressed(_ sender: AnyObject) {
        let button = sender as? UIButton
        let cell = button?.superview?.superview as? UITableViewCell
        let indexPath = veglistTbl.indexPath(for: cell!)
        print(indexPath?.row)


    }

    @objc func DecreasePressed(_ sender: AnyObject) {
           let button = sender as? UIButton
           let cell = button?.superview?.superview as? UITableViewCell
           let indexPath = veglistTbl.indexPath(for: cell!)
           print(indexPath?.row)
       }

在此处输入图像描述

  1. UITableCell Index 得到了如何单独重新加载。

  2. 单击加号按钮和减号按钮应该添加数量,并且计数应该像swiggy一样增加和减少。怎么能做到这一点。我尝试了 UIStepper 和 KWStepper 来实现这一点,但我无法实现这一点。任何帮助实现这一目标。提前谢谢。

4

1 回答 1

1

根据我在您的问题中收集的内容,您已经有了要重新加载的 indexPath。因此,只需使用以下方法重新加载该特定行:

tableView.reloadRows(at: [indexPath], with: .automatic)
于 2020-05-12T11:26:19.863 回答