I have a data in tableview. I have two buttons + and -, i want to increase and decrease value on click of buttons. please check below image to understand exactly.
When i press on first cell's plus icon, the value is reflecting in last cell.
This is my code :-
import UIKit
struct Product {
var price = 0
}
class TicketBookingVC: UIViewController , UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblView: UITableView!
@IBOutlet weak var mainTblView: UIView!
var bookingDetails = NSDictionary()
var paymentDetails = NSDictionary()
var arrPaymentDetails = NSArray()
var productArray = [Product]()
var product : Product!
private var counterValue = 1
var productIndex = 0
var counterLbl = UILabel()
@IBOutlet weak var bookBtn: UIButton!
@IBOutlet weak var eventImg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
tblView.delegate = self
tblView.dataSource = self
let payment = self.paymentDetails.value(forKey: "payment") as! NSArray
self.arrPaymentDetails = payment as NSArray
for _ in 0...10{
productArray.append(Product(price: 1))
}
// Do any additional setup after loading the view.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
}
else if section == 1{
return arrPaymentDetails.count
}
else{
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellfirst", for: indexPath)
cell.selectionStyle = .none
return cell
}
else if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellsecond", for: indexPath)
let mainViewCell = cell.contentView.viewWithTag(2000) as! UIView
let normalView = cell.contentView.viewWithTag(2001) as! UIView
let eventName = cell.contentView.viewWithTag(2003) as! UILabel
let eventPrice = cell.contentView.viewWithTag(2004) as! UILabel
counterLbl = cell.contentView.viewWithTag(2007) as! UILabel
let decrementBtn = cell.contentView.viewWithTag(2005) as! UIButton
let incrementBtn = cell.contentView.viewWithTag(2006) as! UIButton
let dictAllDetails = self.arrPaymentDetails.object(at: indexPath.row) as! NSDictionary
print("dictallgroups : \(dictAllDetails)")
if dictAllDetails.value(forKey: "label") != nil {
eventName.text = dictAllDetails.value(forKey: "label") as! String
}
else{
eventName.text = ""
}
if dictAllDetails.value(forKey: "price") != nil {
eventPrice.text = "₹ \(dictAllDetails.value(forKey: "price") as! String)"
}
else{
eventPrice.text = "₹ 0"
}
decrementBtn.addTarget(self, action:#selector(self.decrementbuttonClicked), for: .touchUpInside)
incrementBtn.addTarget(self, action:#selector(self.incrementbuttonClicked), for: .touchUpInside)
product = productArray[indexPath.row]
counterLbl.text = "\(product.price)"
productIndex = indexPath.row
cell.selectionStyle = .none
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellthird", for: indexPath)
cell.selectionStyle = .none
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0{
return UITableView.automaticDimension
}
else{
return 80
//return UITableView.automaticDimension
}
}
@objc func decrementbuttonClicked() {
print("Button decrement")
if(counterValue != 1){
counterValue -= 1;
}
print("\(counterValue)")
self.counterLbl.text = "\(counterValue)"
product.price = counterValue
print("\(product.price)")
addProductToCart(product: product, atindex: productIndex)
}
@objc func incrementbuttonClicked() {
print("Button increment")
counterValue += 1;
print("\(counterValue)")
self.counterLbl.text = "\(counterValue)"
product.price = counterValue
print("\(product.price)")
addProductToCart(product: product, atindex: productIndex)
}
func addProductToCart(product: Product, atindex: Int) {
productArray[atindex] = product
calculateTotal()
}
func calculateTotal()
{
var totalValue = 0
for objProduct in productArray {
totalValue += objProduct.price
}
// self.totalLabel.text = "Total \(totalValue)"
}
}
I tried this link also :-
How do I increment/decrement a label value with two buttons pressed in tableview Swift
Thank you in advance.