How to Make Adding data labels to table cell and the sum data cell in Swift 4
Please add this
var List: [String] = [""]
var sum = 0
then in your second view controller
after
List.append(textIsi.text!)
add thissum += Int(textIsi.text!)! Like this
List.append(textIsi.text!)
sum += Int(textIsi.text!)!
labelRp.text = "\(sum)"
then full code in InAndDelRowViewController
import UIKit class InAndDelRowViewController: UIViewController { @IBOutlet weak var labelRp: UILabel! @IBOutlet weak var textIsi: UITextField! @IBOutlet weak var bAdd: UIButton! @IBOutlet weak var tblView: UITableView! var List: [String] = [""] var sum = 0 override func viewDidLoad() { super.viewDidLoad() self.tblView.delegate = self self.tblView.dataSource = self self.tblView.rowHeight = UITableViewAutomaticDimension self.tblView.estimatedRowHeight = 100.0 self.tblView.separatorInset = UIEdgeInsetsMake(0, UIScreen.main.bounds.width, 0, 0) self.tblView.register(UINib(nibName: "InDelTableViewCell", bundle: nil), forCellReuseIdentifier: "cell") //membuat tabel bawah kosong (tabel ada klw ada data) tblView.tableFooterView = UIView(frame: CGRect.zero) } @IBAction func bAddAction(_ sender: UIButton) { print(self.List) insertNewVTitle() } func insertNewVTitle() { List.append(textIsi.text!) sum += Int(textIsi.text!)! labelRp.text = "\(sum)" let indexPath = IndexPath(row: List.count - 1, section: 0) tblView.beginUpdates() tblView.insertRows(at: [indexPath], with: .automatic) tblView.endUpdates() textIsi.text = "" view.endEditing(true) } } extension InAndDelRowViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return List.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let Title = List[indexPath.row] let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! InDelTableViewCell cell.labelTest.text = Title return cell } //for edit func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { List.remove(at: indexPath.row) tableView.beginUpdates() tableView.deleteRows(at: [indexPath], with: .automatic) tableView.endUpdates() } } }
No comments: