1

my table view cell

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) {


}


}

(cart tableView) service choice cell

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
}

}

(cart tableView) order total cell

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
}

}

How do I program the stepper to increase the label in value when selected above is the current code I am using but it crashes upon clicking the stepper, also how do I take this value and either send it to the cart vc to multiply with pricing info in the Options array and send the selected items data to the service choice cell in the cart, or multiply it in the Service1ViewController and send the total data to the cart along with sending the selected items data to the service choice cell?

1 Answers1

1

I did sample for Add to Cart screen. User may increase/ decrease their order. Initially, you have to store default values in dictionary. This link is for UISegmentedControl.

In Storyboard itself, set UIStepper's minimumValue, maximumValue and Step

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()
}
McDonal_11
  • 3,935
  • 6
  • 24
  • 55
  • confusing I need to explain more – Omar Al-Eisa Feb 07 '18 at 06:28
  • Action for UIStepper should be in UIViewController. Instead of **cell.quantityStepper.addTarget(self, action: #selector(setter: cell.stepperLabel.text), for: .valueChanged) ** – McDonal_11 Feb 07 '18 at 06:29
  • I revised the post to make my question understandable, I appreciate your fast response but not everything you posted pertains to my entire question – Omar Al-Eisa Feb 07 '18 at 06:45
  • 1. cell.stepper = step[indexPath.row]. what is this ? 2. @IBAction func valueChanged(_ sender: AnyObject) { print("Method .valueChanged activated") //Update stepperlabel stepperLabel.text = String(Int(quantityStepper.value)) } Do this action in UIViewController, not in UITableViewCell – McDonal_11 Feb 07 '18 at 06:51
  • ok I understand I have to perform that action in the UIViewController, but about the rest of the question such as multiplying that value by the pricing integer (thats also another issue should I use double to display decimals) and moving the data to the cartVC – Omar Al-Eisa Feb 07 '18 at 07:02
  • Price 5.29rs. If user increase by 1, It should display 10.58 ?? am I right ? – McDonal_11 Feb 07 '18 at 07:07
  • no the stepperLabel displays an integer which I would like to take the per specific cell and multiply it by the pricing value for that specific cell (this is done for all cells) then display the total in service1Total UILabel and forward the data of the cells with a stepperLabel value above 0 to the cart in ( service choice cell) and the service1Total to the (order total cell) – Omar Al-Eisa Feb 07 '18 at 07:15
  • Can u post ur Screen design ? – McDonal_11 Feb 07 '18 at 07:19
  • can I send it to you privately? – Omar Al-Eisa Feb 07 '18 at 07:23
  • Ok. Give ur mail id – McDonal_11 Feb 07 '18 at 07:27
  • buildyoursoftware@icloud.com – Omar Al-Eisa Feb 07 '18 at 07:28
  • Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)' I get this error from inputing tableView.reloadData() – Omar Al-Eisa Feb 08 '18 at 22:49