stepper增加label的值,然后乘以init函数中的值从array中

时间:2018-02-07 03:26:56

标签: ios swift uitableview

我的表格视图单元格

class Service1TableViewCell: UITableViewCell {

@IBOutlet weak var quantityStepper: UIStepper!
@IBOutlet weak var Service1image: UIImageView!
@IBOutlet weak var serviceTitle: UILabel!
@IBOutlet weak var pricingInfo: UILabel!
@IBOutlet weak var stepperLabel: UILabel!



func setService1(Dry: Service1){
    Service1image.image = Dry.service1image
    pricingInfo.tag = Dry.service1Pricing
    serviceTitle.text = Dry.service1Title
}
//save data from each cell and multiply for total service


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
@IBAction func valueChanged(_ sender: AnyObject) {
    print("Method .valueChanged activated")

    //Update stepperlabel
    stepperLabel.text = String(Int(quantityStepper.value))

}
}

的ViewController

class Service1ViewController: UIViewController {

@IBOutlet weak var service1TableView: UITableView!

@IBOutlet weak var service1Total: UILabel!


var Dry: [Service1] = []

var step = [Stepper]()

override func viewDidLoad() {
    super.viewDidLoad()

    Dry = Options()



}
func Options() -> [Service1]{

    var dryOptions: [Service1] = []

    let option1 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option2 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option3 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option4 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option5 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option6 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option7 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option8 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option9 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option10 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option11 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option12 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option13 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option14 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option15 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option16 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option17 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option18 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option19 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option20 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option21 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option22 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option23 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option24 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option25 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option26 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option27 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option28 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option29 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option30 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))
    let option31 = Service1(titled: "Test", pricing: Int(5.29), image: #imageLiteral(resourceName: "Test"))

    dryOptions.append(option1)
    dryOptions.append(option2)
    dryOptions.append(option3)
    dryOptions.append(option4)
    dryOptions.append(option5)
    dryOptions.append(option6)
    dryOptions.append(option7)
    dryOptions.append(option8)
    dryOptions.append(option9)
    dryOptions.append(option10)
    dryOptions.append(option11)
    dryOptions.append(option12)
    dryOptions.append(option13)
    dryOptions.append(option14)
    dryOptions.append(option15)
    dryOptions.append(option16)
    dryOptions.append(option17)
    dryOptions.append(option18)
    dryOptions.append(option19)
    dryOptions.append(option20)
    dryOptions.append(option21)
    dryOptions.append(option22)
    dryOptions.append(option23)
    dryOptions.append(option24)
    dryOptions.append(option25)
    dryOptions.append(option26)
    dryOptions.append(option27)
    dryOptions.append(option28)
    dryOptions.append(option29)
    dryOptions.append(option30)
    dryOptions.append(option31)

    return dryOptions

}

}
extension Service1ViewController: UITableViewDataSource, UITableViewDelegate{


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return Dry.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let Dry1 = Dry[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "Service1Cell") as! Service1TableViewCell
    cell.setService1(Dry: Dry1)
    cell.stepper = step[indexPath.row]
    return cell
}

// send selected data to cart
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


}


}

(购物车tableView)服务选择单元

class CartTableViewCell: UITableViewCell {

@IBOutlet weak var serviceChoiceImage: UIImageView!
@IBOutlet weak var serviceChoiceTitle: UILabel!
@IBOutlet weak var serviceChoiceAmount:UILabel!
@IBOutlet weak var serviceChoicePrice: UILabel!



override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

(购物车tableView)订购总单元

class CartTotalTableViewCell: UITableViewCell {

@IBOutlet weak var orderTotal: UILabel!
@IBOutlet weak var OrderTax: UILabel!
@IBOutlet weak var deliveryFee: UILabel!
@IBOutlet weak var grandTotal: UILabel!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

当我选择上面的当前代码时,如何编程步进器以增加标签值,但是在单击步进器时它会崩溃,我该如何获取此值并将其发送到购物车vc以相乘使用Options数组中的定价信息并将所选项目数据发送到购物车中的服务选择单元格,或者将其乘以Service1ViewController并将总数据发送到购物车,同时将所选项目数据发送到服务选择单元格? / p>

1 个答案:

答案 0 :(得分:1)

我为“添加到购物车”屏幕做了示例。用户可以增加/减少他们的订单。最初,您必须在dictionary中存储默认值。 This链接适用于UISegmentedControl

Storyboard内,设置UIStepper's minimumValuemaximumValueStep

var orderCount = [Int : Int]()
override func viewDidLoad() {
    super.viewDidLoad()

    for i in 0...29  // TotaL number of rows
    {
       orderCount[i] = 1 // DEFAULT order count will be 1
    }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 30
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SOTableViewCell
    cell.myStepper.tag = indexPath.row // ADDING TAG
    cell.testLbl.text = String(orderCount[indexPath.row]!) // PASS VALUES TO LABEL
    cell.myStepper.value = Double(orderCount[indexPath.row]!) // PASS VALUES TO STEPPER
    cell.selectionStyle = .none
    return cell
}


//ACTION should be in this ViewController. 
@IBAction func stepperAcn(_ sender: UIStepper) {

    print("sender.value         ", sender.value)
    let getTag : Int = sender.tag

    orderCount[getTag] = Int(sender.value)

    tblVw.reloadData()
}