soRazor
soRazor

Reputation: 147

UITableView wont refresh after adding a new entry

From my original view controller (BudgetViewController) I have a modal view controller that pops up when the "add" UIButton is selected. The user selects a new category and AddCategory() is dismissed. I have tried refreshing the UITableView inside of BudgetViewController() by adding self.tableView.reloadData() inside of viewWillAppear(), viewDidAppear, and also as a completion handler but it still refuses to refresh the UITableView.

 class BudgetViewController: UIViewController {

    var budgetData: [Transaction] = [
    Transaction(title: "Groceries", dateInfo: "0% out of spent", image:UIImage.groceriesIcon, amount: 4512)
                             ]

 func didTapAddCategory(_ button: UIButton) {
    let modalViewController = AddCategory()
    modalViewController.addCategoryCompletion = {
        self.tableView.reloadData()
       
    }
    present(modalViewController, animated: false, completion: modalViewController.addCategoryCompletion)
}


   override func viewDidAppear(_ animated: Bool) {
    self.tableView.reloadData()
        }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.budgetData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: CategoryCell.identifier) as? CategoryCell else { return UITableViewCell() }
    
    let data = budgetData[indexPath.row]
    cell.populate(with: data)
    cell.spentLabel.text = "-"
    cell.rangeLabel.text = ""
    return cell
}



   class AddCategory: UIViewController{

      let budgetVC = BudgetViewController()

 var addCategoryCompletion: (() -> Void)?


@objc func prin(){
    budgetVC.budgetData.append(Transaction(title: textField.text ?? "", dateInfo: "n", image: UIImage.gymIcon, amount: 12, annualPercentageRate: 12, trailingSubText: "12"))

    self.dismiss(animated: false, completion: addCategoryCompletion)
    print("selected")
    print(budgetVC.budgetData)
}
  let categoryTF: UITextField = {
    let tf = UITextField()
    
    tf.placeholder = "  Category name"
    tf.font = UIFont.systemFont(ofSize: 14)
    return tf
}()

 let saveButton: UIButton = {
     let button = UIButton()
     button.setTitle("Save", for: .normal)
     button.layer.cornerRadius = 7
     button.addTarget(self, action: #selector(prin), for:.touchUpInside)
    return button
     
 }()

 }

Upvotes: 0

Views: 50

Answers (1)

Mikhail Vasilev
Mikhail Vasilev

Reputation: 760

As already mentioned, from BudgetVC number 1 you create AddcategoryVC, and inside it you create BudgetVC number 2. So, calling reload data for BudgetVC number 2 does nothing.

Using callback is the most common solution to this: use

var addCategoryCompletion: ((Transaction) -> Void)?

and remove

let budgetVC = BudgetViewController()

inside AddCategory


Set completion in didTapAddCategory

let modalViewController = AddCategory()
modalViewController.addCategoryCompletion = { newTransaction in
    self.budgetData.append(newTransaction)
    self.tableView.reloadData()
}

And finally call this completion callback in dismiss action inside prin method

let newTransaction = Transaction(title: textField.text ?? "",
                                 dateInfo: "n",
                                 image: UIImage.gymIcon,
                                 amount: 12,
                                 annualPercentageRate: 12,
                                 trailingSubText: "12")

self.dismiss(animated: false, completion: {
            self.addCategoryCompletion?(newTransaction)
})

Upvotes: 1

Related Questions