Reputation: 81
I am trying to take the values that I add to the tableview
and add them to my totalLabel
. I have attached my code and a screenshot below to help. In the example, it does not add to the totalLabel
. item 1 plus item 2 should be $2 and let's say I add another item for $5 then it would be $7.
import UIKit
import AVFoundation
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var totalLabel: UILabel!
var items: [Item] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func addItem(_ sender: Any) {
let alert = UIAlertController(title: "Add Item", message: "Add item And Price", preferredStyle: .alert)
alert.addTextField { (UITextField) in
UITextField.placeholder = "Enter Item Name"
}
alert.addTextField { (UITextField) in
UITextField.placeholder = "Enter Item Price"
UITextField.keyboardType = .numbersAndPunctuation
}
alert.addAction(UIAlertAction(title: "Add", style: .default, handler: { (UIAlertAction) in
let content = alert.textFields![0] as UITextField
let content2 = alert.textFields![1] as UITextField
let value = "\(content2.text!)"
let item = Item(itemName: "\(content.text!)", itemPrice: Double(value)!)
self.items.append(item)
self.tableView.reloadData()
guard let total = Double(self.totalLabel.text!) else { return }
let sum = String(total + Double(value)!)
self.totalLabel.text = sum
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let items = self.items[indexPath.row]
cell.textLabel?.text = items.itemName
cell.detailTextLabel?.text = "$\(String(items.itemPrice))"
var totalAmount = 0.0
for value in self.items {
totalAmount = totalAmount + items.itemPrice
print(value)
print(totalAmount)
}
totalLabel.text = "Total is $\(totalAmount)"
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Grocery List"
}
}
Upvotes: 1
Views: 1693
Reputation: 285150
As already mentioned in my answer to your previous question you have to sum up the price
values in the items
array
Create a function which reloads the table view and updates the total
label
func reloadAll() {
tableView.reloadData()
let totalAmount = String(items.map{$0.itemPrice}.reduce(0.0, +))
totalLabel.text = "Total is $\(totalAmount)"
}
Then call this method everywhere you call tableView.reloadData()
for example replace these lines in the alert action
self.tableView.reloadData()
guard let total = Double(self.totalLabel.text!) else { return }
let sum = String(total + Double(value)!)
self.totalLabel.text = sum
just with
self.reloadAll()
And delete the following lines in cellForRow
. It's pointless to update the label in this method because the same computation is performed items.count
times (the loop runs items.count²
times!)
var totalAmount = 0.0
for value in self.items {
totalAmount = totalAmount + items.itemPrice
print(value)
print(totalAmount)
}
totalLabel.text = "Total is $\(totalAmount)"
Upvotes: 3
Reputation: 998
just replace this for loop instead of existing loop in cellForRowAt
for value in self.items {
totalAmount = totalAmount + value.itemPrice
}
Upvotes: 2
Reputation: 1105
Update this method
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let items = self.items[indexPath.row]
cell.textLabel?.text = items.itemName
cell.detailTextLabel?.text = "$\(String(items.itemPrice))"
var totalAmount = 0.0
for value in self.items {
totalAmount = totalAmount + items.itemPrice
}
totalLabel.text = "Total is $\(totalAmount)"
return cell
}
I hope this is what you looking.
Upvotes: 0