i've a array of object in my app that is passed to another VC called restaurantViewController the value is stored in restMenu and now the problem is that whenever i scroll the UITableView the ItemQuantityCell value is always reused to 0 which is default value in my restMenu how can i update value in my restMenu so i can pass it to checkoutVC with correct value for my ItemQuantityCell. my plist from where the data is coming is as follows
here is my code
var restMenu = [[String:Any]]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! RestaurantItemViewCell
cell.delegate = self as? TableViewCellDelegate
cell.selectionStyle = UITableViewCellSelectionStyle.none
//assign item name to cell
let selectedDictName = restMenu[indexPath.row] as NSDictionary
print("the selected dict ", selectedDictName)
cell.itemNameLabel.text = selectedDictName.value(forKey: "ItemName") as? String
// assign item price to cell
let selectedDictPrice = restMenu[indexPath.row] as NSDictionary
cell.itemPriceLabel.text = "Price: \(selectedDictPrice.value(forKey: "ItemPrice") as! String)"
// assign item quantity to cell
let selectedDictQuant = restMenu[indexPath.row] as NSDictionary
cell.itemQuantityLabel.text = selectedDictQuant.value(forKey: "ItemQuant") as? String
}
As per your plist, ItemQuant is zero always and you are assigning the same value in cell.itemQuantityLabel.text
so it will be always zero.
Next, please do changes in your code, do not use NSStuff in Swift.
Update your cellForRowAt indexPath
like below
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! RestaurantItemViewCell
cell.delegate = self as? TableViewCellDelegate
cell.selectionStyle = UITableViewCellSelectionStyle.none
//assign item name to cell
let dict = restMenu[indexPath.row]
print("the selected dict ", dict)
cell.itemNameLabel.text = dict["ItemName"] as! String
cell.itemPriceLabel.text = dict["ItemPrice"] as! String
cell.itemQuantityLabel.text = dict["ItemQuant"] as! String
return cell
}
Hope this works for you.
FYI. Above code is not tested. It is for your reference only.
Note Wherever you want to update the quantity of item Get the index on which you want to update the quantity and do as follows
restMenu[index]["ItemQuant"] = "2" // index is you will get somehow & 2 is just for example you can do what you want there.
UPDATE
As per your last comment, this is suggestion.
Instead of Dictionary Structure, create appropriate model classes, and parse the plist, your cellForRowAt indexPath
will be changed, Your ItemQuant must be Int value. Pass the restMenu
object to other ViewController
and update its ItemQuant. If you navigate back just reload your tableview data. It will show you changes in ItemQuant.