Posts

Post marked as solved
3 Replies
566 Views
I have a view controller which has a child view. I want to convey information from the view to the child view. To do so, I did this : func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { performSegue(withIdentifier: "visualize", sender: self) invoiceNumber = indexPath.row } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let vc = segue.destination as! PreviewViewController vc.invoiceNumber = invoiceNumber }But the problem is that the value of invoiceNumber is not updated on the first iteration but on the second.Here is the code of printing : import UIKit class ViewController: UIViewController { var invoiceNumber: Int = 0 override func viewDidLoad() { super.viewDidLoad() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) print(invoiceNumber) } } var customerData: [[customerInformation]] = [] var itemsData: [[Item]] = [] var totalData: [TotalInformation] = [] var invoiceNumber = 0 override func viewDidLoad() { super.viewDidLoad() tableView.reloadData() } override func viewDidAppear(_ animated: Bool) { let tabBar = tabBarController as! baseTabBarController customerData = tabBar.customerData itemsData = tabBar.itemsData totalData = tabBar.totalData tableView.reloadData() } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return customerData.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let invoiceCell = tableView.dequeueReusableCell(withIdentifier: "invoice", for: indexPath) as! invoiceTableViewCell invoiceCell.textLabel?.text = (customerData[indexPath.row][0]).input invoiceCell.detailTextLabel?.text = "Invoice n°" + String(indexPath.row) invoiceCell.totalLabel.text = (totalData[indexPath.row]).total return invoiceCell } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { let tabBar = tabBarController as! baseTabBarController tabBar.customerData.remove(at: indexPath.row) //itemData.remove(at: indexPath.row) tabBar.totalData.remove(at: indexPath.row) customerData = tabBar.customerData itemsData = tabBar.itemsData totalData = tabBar.totalData tableView.deleteRows(at: [indexPath], with: .fade) } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { invoiceNumber = indexPath.row print(invoiceNumber, "yes") performSegue(withIdentifier: "visualize", sender: self) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let vc = segue.destination as! PreviewViewController vc.invoiceNumber = invoiceNumber } }It is the "vc.invoiceNumber = invoiceNumber" which comes before the "invoiceNumber = indexPath.row" as I tried to change the value of invoiceNumber a variable in the view and print out the value in the child view and if showed its value first (I tested -1).I added the Preview view controller which receives the invoiceNumber : import UIKit import WebKit class PreviewViewController: UIViewController { @IBOutlet var webPreview: UIWebView! var invoiceComposer: InvoiceComposer! var HTMLContent: String! var invoiceNumber: Int = -1 override func viewDidLoad() { super.viewDidLoad() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) print(invoiceNumber) createInvoiceAsHTML() } func createInvoiceAsHTML() { invoiceComposer = InvoiceComposer() if let tabBar = tabBarController as? baseTabBarController { if let invoiceHTML = invoiceComposer.renderInvoice(invoiceNumber: String(invoiceNumber), invoiceDate: "", recipientInfo: tabBar.customerData[invoiceNumber][0].input, items: tabBar.itemsData[invoiceNumber], totalAmount: tabBar.totalData[invoiceNumber].total) { webPreview.loadHTMLString(invoiceHTML, baseURL: NSURL(string: invoiceComposer.pathToInvoiceHTMLTemplate!)! as URL) HTMLContent = invoiceHTML } } else { print("tabBarController is not of type baseTabBarController or either nil ") } } }The "vc.invoiceNumber = invoiceNumber" runs before the "invoiceNumber = indexPath.row" but I want the inverse.
Posted
by m4thus4n.
Last updated
.
Post marked as solved
1 Replies
617 Views
I heard about the new swift DSL by the end of this video : https://developer.apple.com/videos/play/wwdc2019/402/. I am interested in it as I am doing an application where the user can create a HTML file. But I couldn't find any reference and I don't even know how to implement it in swift. Please help ! Thanks
Posted
by m4thus4n.
Last updated
.
Post not yet marked as solved
7 Replies
1.5k Views
I have a custom table view cell which contains a label. I want to update that label whenever a the plus or the minus button of the stepper is actioned. But the label is updated. I can print the label's value but not set it. Please help ! Thanks.import UIKit import CoreData class newInvoiceViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet var tableView: UITableView! var itemList: [Item] = [] var cells: [[Any]] = [] var items: [Any] = [] override func viewDidLoad() { super.viewDidLoad() cells = [[customerInformation(placeholder: "Name"), customerInformation(placeholder: "Address"), customerInformation( } func updateTotalLabel() -> Double { var total = 0.0 for item in self.cells[1] { let price = Double((item as! Item).price) let quantity = Double((item as! Item).quantity + 1) total += price! * quantity } return total } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let itemCell = tableView.dequeueReusableCell(withIdentifier: "item", for: indexPath) as! itemTableViewCell let totalCell = tableView.dequeueReusableCell(withIdentifier: "total", for: indexPath) as! totalTableViewCell if cells[indexPath.section][indexPath.row] is Item { itemCell.callBackOnStepperPlus = { itemCell.detailTextLabel!.text = String(Int((self.cells[1][indexPath.row] as! Item).counter)) let total = self.updateTotalLabel() totalCell.totalLabel!.text = "changed" } itemCell.callBackOnStepperMinus = { if Int((self.cells[1][indexPath.row] as! Item).counter) > 1 { itemCell.detailTextLabel!.text = String(Int((self.cells[1][indexPath.row] as! Item).counter)) let total = self.updateTotalLabel() } } return itemCell } else if (cells[indexPath.section][0] as! String) == "1" { return newItemCell } else { return totalCell } } }The name of the label is totalLabel. Everthing works fine (no error) but unable to set label's value.
Posted
by m4thus4n.
Last updated
.