Reputation: 17
I want to show value on UIlabel. when I press UIbutton to increase or decrease values on label. This is my code and when I am running my project but I didn't get any value on my uilabel.
@IBAction func btnIncreaseAction(_ sender: Any) {
var count = 0
count = (count + 1)
if let cell = (sender as? UIButton)?.superview?.superview?.superview as? ShoppingCell
{
//cell.lblForOnty.text = "\(cell.lblForOnty.text ?? 0 + 1)"
cell.lblForOnty.text = String(count)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! ShoppingCell
var someValue: Int = 0 {
didSet {
cell.lblForOnty.text = "\(count)"
}
}
return cell
}
}
Upvotes: 0
Views: 5440
Reputation: 1055
Please follows these step:
Step 1 :
create object class
class QuantityBO : NSObject{
var quantity : Int?
}
step 2: Create global array in your class
var arrQuantityList = [QuantityBO]()
step 3 :
Assign the value according to your number of cell
It may be change according to your api resonse
step 4:
In cellForRowAt method please write:
cell.lblQuantity.text = String(arrQuantityList[indexPath.row].quantity)
cell.yourBtnName.tag = indexPath.row
step 5:
On your button action
arrQuantityList[sender.tag].quantity = arrQuantityList[sender.tag].quantity + 1
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.none)
It may helps to you.hank you.
Upvotes: 0
Reputation: 285082
superview?.superview?.superview
is pretty weird. Don't do that. A callback is more reliable.
In the subclass ShoppingCell
create a callback and the IBAction
s for in- and decrease . Connect the actions to the buttons
class ShoppingCell: UITableViewCell {
@IBOutlet weak var lblForOnty: UILabel!
var callback : ((Int)->())?
var counter = 0 {
didSet {
lblForOnty.text = "\(count)"
}
}
@IBAction func btnIncreaseAction(_ sender: UIButton) {
counter += 1
callback?(counter)
}
@IBAction func btnDecreaseAction(_ sender: UIButton) {
if counter > 0 { counter -= 1 }
callback?(counter)
}
}
In cellForRow
pass the counter
value to the cell and use the callback to update the model which is represented by dataSource
and which is a custom struct or class containing a property counter
.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID", for: indexPath) as! ShoppingCell
let item = dataSource[indexPath.row]
cell.counter = item.counter
cell.callback = ( newValue in
item.counter = newValue
}
return cell
}
No hassle with superviews and index paths.
Upvotes: 2
Reputation: 4891
This is the code which should be in your ViewController
. I am assuming that numberOfSections
is 1 and in numberOfRowsInSection
you are passing then number of rows you want. Else you need to modify this line : let indexPath = IndexPath(row: sender.tag, section: 0)
.
var count = 0 // Count variable should be a global variable, you need it to decrease the value too.
@objc func increaseCounter(sender: UIButton) {
//increase logic here
count = (count + 1)
let indexPath = IndexPath(row: sender.tag, section: 0)
let cell = tableView.cellForRow(at: indexPath)
cell.lblForOnty.text = "\(count)"
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! ShoppingCell
// Add tag and action to your button
cell.yourButton.tag = indexPath.row
cell.yourButton.addTarget(self, action: #selector(increaseCounter(sender:)), for: .touchUpInside)
return cell
}
Upvotes: 2
Reputation: 41
Move you count variable outside of the function Increment/decrement the count inside the function and reload the table or you can reload particular index as well. PFB the code snipped
let indexPath = NSIndexPath(forRow: rowNumber, inSection: 0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
Upvotes: 1