ios - Swift 中购物应用程序的加号和减号按钮

标签 ios swift

我遇到了问题。我找不到使这些按钮正常工作的方法,我需要一些帮助。

我正在尝试为一家餐厅制作一个应用程序,但我不知道如何使加号和减号按钮起作用。我从 Firebase 中提取数据(产品的名称和价格)。我有一个金额标签。点击加金额增加,点击减金额减少。

This is how it looks like

这是我的 viewController 中的代码:

import UIKit
import Firebase
import FirebaseDatabase

class foodListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var plus: UIButton!
@IBOutlet weak var foodTableView: UITableView!
var ref:DatabaseReference?
var databaseHandle: DatabaseHandle?
var foodData = [food]()
var stats = [Buy]()

override func viewDidLoad() {
    super.viewDidLoad()

    foodTableView.delegate = self
    foodTableView.dataSource = self
    foodTableView.backgroundView = UIImageView(image: UIImage(named: "bg-general.png"))
    foodTableView.allowsSelection = false
    foodTableView.separatorStyle = .none

    //Set the firebase reference
    ref = Database.database().reference()

    //Retrieve the data and listen for changes
ref?.child("inventory").child("food").observe(.childAdded, with: { (snapshot) in
   /*    if let dict = snapshot.value as? [String: AnyObject] {
        let foods = food()
            foods.setValuesForKeys(dict)
            print(foods.FirstName!)
            //self.foodTableView.reloadData()
    }*/
    print(snapshot)
    if let dictionary = snapshot.value as? [String: AnyObject] {
        let foods = food()
       // foods.setValuesForKeys(dictionary)
        foods.title = dictionary["title"] as? String
        foods.amount = dictionary["amount"] as? String
        foods.price = dictionary["price"] as? Double
        foods.category = dictionary["category"] as? String
        foods.id = dictionary["id"] as? String

        self.foodData.append(foods)


       DispatchQueue.main.async {
            print("BlaBlaBla")
            self.foodTableView.reloadData()
       }
      //  print(foods.title!,foods.amount!,foods.price!,foods.category!,foods.id!)

    }

    })

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return foodData.count
}

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

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

    let food = foodData[indexPath.row]
    // Food and price
    let titleLabel = cell.viewWithTag(1) as! UILabel
    let priceLabel = cell.viewWithTag(2) as! UILabel
    let cantitateLabel = cell.viewWithTag(3) as! UILabel

    //Labels text size
   // titleLabel.font = UIFont(name: "Times New Roman-Bold", size: 30)
  //  priceLabel.font = UIFont(name: "Times New Roman-Bold", size: 17.0)
  //  cantitateLabel.font = UIFont(name: "Times New Roman-Bold", size: 17.0)



    titleLabel.text = food.title
    let numberFormatter = NumberFormatter()
    numberFormatter.numberStyle = .decimal
    priceLabel.text = numberFormatter.string(from: food.price! as NSNumber)! + " $"

    // Design for table
    cell.backgroundColor = UIColor.clear
    cell.contentView.backgroundColor = UIColor.clear
    let whiteRoundedView : UIView = UIView(frame: CGRect(x: 0, y: 10, width: self.view.frame.size.width, height: 70))
    whiteRoundedView.backgroundColor = UIColor.greenColor
    whiteRoundedView.layer.masksToBounds = false
    whiteRoundedView.layer.cornerRadius = 3.0
    whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
    whiteRoundedView.layer.shadowOpacity = 0.5
    cell.contentView.addSubview(whiteRoundedView)
    cell.contentView.sendSubview(toBack: whiteRoundedView)

    //Plus - Minus
    let pluss = cell.viewWithTag(4) as! UIButton
    let minuss = cell.viewWithTag(5) as! UIButton

    //pluss.addTarget(self, action: #selector(plusss(cantitate: amount)), for: .touchUpInside)
  //  minuss.addTarget(self, action: #selector(minusss(cantitate: amount)), for:. touchUpInside)



    return cell

}

}

此外,按加号会将我的产品添加到另一个用于我的订单列表的viewcontroller。按减号将减少金额,当金额为 0 时,将从订单列表中删除我的产品。

提前致谢。

最佳答案

您需要一个用于此类按钮的 IBAction...我建议您拥有一个带有按钮和标签的原型(prototype) tableViewCell,并在相应的 View Controller 中处理操作。

关于ios - Swift 中购物应用程序的加号和减号按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51657448/

相关文章:

ios - NSAttributedString 如何使用 NSParagraphStyle 设置文本行居中对齐

ios - Metal 中的纹理阵列

ios - 更改 UITabBarController 中选项卡的宽度以调整第六个选项卡

ios - GoogleMaps Swift 点击标记

ios - 套接字连接()即使在对等重置后也返回 0

ios - 如何在ios中的缓存文件路径中保存多个xml文件?

ios - CLLocation 列表中的地点/城市名称

ios - 在 UICollectionViewCell 中使用 Storyboard 的自动布局约束

ios - 在弱指针上调用 getter 并将其传递给方法时保留循环可能性

ios - AFNetworking + Google Places API Web服务+具有多个baseURL的Parse SDK