Ios swift中购物应用程序的加号和减号按钮
我有个问题。我找不到办法使这些按钮正常工作,我需要一些帮助 我正在尝试为一家餐厅制作一个应用程序,但我不知道如何让加号和减号按钮工作。我从Firebase中提取数据(产品的名称和价格)。我有一个数量标签。单击加金额增加,单击减金额减少 以下是我的viewController中的代码:Ios swift中购物应用程序的加号和减号按钮,ios,swift,Ios,Swift,我有个问题。我找不到办法使这些按钮正常工作,我需要一些帮助 我正在尝试为一家餐厅制作一个应用程序,但我不知道如何让加号和减号按钮工作。我从Firebase中提取数据(产品的名称和价格)。我有一个数量标签。单击加金额增加,单击减金额减少 以下是我的viewController中的代码: import UIKit import Firebase import FirebaseDatabase class foodListViewController: UIViewController, UITa
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
}
}
另外,按plus将把我的产品添加到另一个viewcontroller
,它是我的订单列表。按减号将减少金额,当金额为0时,它将从订单列表中删除我的产品
提前感谢。您需要一个iAction来设置这些按钮。。。我建议您使用带有按钮和标签的原型tableViewCell,并在相应的视图控制器中处理操作。这就是我如何管理两个不同的按钮以充当步进器的方法 存储需要更新的值的我的数组
var meanValuesArray : [String] = ["","","0","0","0","1","1","",""]
//MARK: math Function Enum
/**
This Enum is used to Detect do Add Math Operation or Subtract action is to be Performed
*/
enum mathFunction {
/// When Addition is to done
case Add
/// When Subtraction is to be Done
case Subtract
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.addPropertyTableView.dequeueReusableCell(withIdentifier: "APStepperSwitch", for: indexPath) as! addPropertyWithStepperSwitch
cell.staticImageView.image = self.leftImagesArray[indexPath.row]
cell.propertyDetailLabel.text = self.leftValueArray[indexPath.row]
cell.propertyStepperLabel.text = self.meanValuesArray[indexPath.row]
/// Adding Button Handlers
cell.propertyPlusButton.addTarget(self, action: #selector(AddingPropertyVC.plusButtonHandler(sender:)), for: UIControlEvents.touchUpInside)
cell.propertyMinusButton.addTarget(self, action: #selector(AddingPropertyVC.minusButtonHandler(sender:)), for: UIControlEvents.touchUpInside)
return cell
}
用于证明要执行的任务的枚举函数
var meanValuesArray : [String] = ["","","0","0","0","1","1","",""]
//MARK: math Function Enum
/**
This Enum is used to Detect do Add Math Operation or Subtract action is to be Performed
*/
enum mathFunction {
/// When Addition is to done
case Add
/// When Subtraction is to be Done
case Subtract
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.addPropertyTableView.dequeueReusableCell(withIdentifier: "APStepperSwitch", for: indexPath) as! addPropertyWithStepperSwitch
cell.staticImageView.image = self.leftImagesArray[indexPath.row]
cell.propertyDetailLabel.text = self.leftValueArray[indexPath.row]
cell.propertyStepperLabel.text = self.meanValuesArray[indexPath.row]
/// Adding Button Handlers
cell.propertyPlusButton.addTarget(self, action: #selector(AddingPropertyVC.plusButtonHandler(sender:)), for: UIControlEvents.touchUpInside)
cell.propertyMinusButton.addTarget(self, action: #selector(AddingPropertyVC.minusButtonHandler(sender:)), for: UIControlEvents.touchUpInside)
return cell
}
行方法单元格
var meanValuesArray : [String] = ["","","0","0","0","1","1","",""]
//MARK: math Function Enum
/**
This Enum is used to Detect do Add Math Operation or Subtract action is to be Performed
*/
enum mathFunction {
/// When Addition is to done
case Add
/// When Subtraction is to be Done
case Subtract
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.addPropertyTableView.dequeueReusableCell(withIdentifier: "APStepperSwitch", for: indexPath) as! addPropertyWithStepperSwitch
cell.staticImageView.image = self.leftImagesArray[indexPath.row]
cell.propertyDetailLabel.text = self.leftValueArray[indexPath.row]
cell.propertyStepperLabel.text = self.meanValuesArray[indexPath.row]
/// Adding Button Handlers
cell.propertyPlusButton.addTarget(self, action: #selector(AddingPropertyVC.plusButtonHandler(sender:)), for: UIControlEvents.touchUpInside)
cell.propertyMinusButton.addTarget(self, action: #selector(AddingPropertyVC.minusButtonHandler(sender:)), for: UIControlEvents.touchUpInside)
return cell
}
按钮操作
@objc func minusButtonHandler(sender: UIButton) {
/// Getting The Button Position Which is clicked
let buttonPosition : CGPoint = sender.convert(CGPoint.zero, to: self.addPropertyTableView)
/// Getting Index Path From Button Location
let indexPath : IndexPath = self.addPropertyTableView.indexPathForRow(at: buttonPosition)!
/// Extracting and Updating my current Order Value Either + or -
self.meanValuesArray[indexPath.row] = AFWrapperClass.compareStringValue(currentValue: self.meanValuesArray[indexPath.row], limit: 20, toDo: .Subtract)
/// Reloading Table
self.addPropertyTableView.reloadData()
}
@objc func plusButtonHandler(sender: UIButton) {
/// Getting The Button Position Which is clicked
let buttonPosition : CGPoint = sender.convert(CGPoint.zero, to: self.addPropertyTableView)
/// Getting Index Path From Button Location
let indexPath : IndexPath = self.addPropertyTableView.indexPathForRow(at: buttonPosition)!
/// Extracting and Updating my current Order Value Either + or -
self.meanValuesArray[indexPath.row] = AFWrapperClass.compareStringValue(currentValue: self.meanValuesArray[indexPath.row], limit: 20, toDo: .Add)
/// Reloading Table
self.addPropertyTableView.reloadData()
}
主步进功能
class AFWrapperClass : NSObject {
//MARK: Fucntion used to comapre and update value
/**
This function is used to update stepper values
- parameter currentValue : Current Value in Array
- parameter limit : Maximum Value that can be used as stepper+1
- parameter toDo : tells need to perform Add or subtract
*/
class func compareStringValue(currentValue:String, limit:Int, toDo : mathFunction) -> String {
var current : Int = Int(currentValue)!
if (current <= limit) && (current >= 0) {
if toDo == .Add {
if current == limit {
return String(current)
}
else{
current += 1
return String(current)
}
}
else {
if current == 0 {
return String(current)
}
else {
current -= 1
return String(current)
}
}
}
else {
return String(current)
}
}
}
class AFWrapperClass:NSObject{
//标记:用于复制和更新值的功能
/**
此功能用于更新步进器值
-参数currentValue:数组中的当前值
-参数限制:可作为步进器+1使用的最大值
-参数toDo:告知需要执行加法或减法
*/
类func compareStringValue(currentValue:String,limit:Int,toDo:mathFunction)->String{
var电流:Int=Int(currentValue)!
如果(当前=0){
如果toDo==。添加{
如果当前==限制{
返回字符串(当前)
}
否则{
电流+=1
返回字符串(当前)
}
}
否则{
如果当前==0{
返回字符串(当前)
}
否则{
电流-=1
返回字符串(当前)
}
}
}
否则{
返回字符串(当前)
}
}
}
请仅在问题中发布相关代码。对阅读它的人来说,仅仅转储整个文件并让他们阅读是不尊重的。请读,请看。它使用步进器,但处理抽头的方法是相同的。这个问题解决了吗?