Ios UITableView在添加新条目后不会刷新

Ios UITableView在添加新条目后不会刷新,ios,swift,uitableview,Ios,Swift,Uitableview,从我的原始视图控制器(BudgetViewController)中,我有一个模式视图控制器,当选择“添加”按钮时,它会弹出。用户选择一个新类别,AddCategory()将被取消。我尝试通过在ViewWillAspect()和ViewDidAspect()中添加self.tableView.reloadData()并将其作为完成处理程序来刷新BudgetViewController()中的UITableView,但它仍然拒绝刷新UITableView class BudgetViewContr

从我的原始视图控制器(BudgetViewController)中,我有一个模式视图控制器,当选择“添加”按钮时,它会弹出。用户选择一个新类别,AddCategory()将被取消。我尝试通过在ViewWillAspect()和ViewDidAspect()中添加self.tableView.reloadData()并将其作为完成处理程序来刷新BudgetViewController()中的UITableView,但它仍然拒绝刷新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
     
 }()

 }

如前所述,从
BudgetVC编号1
创建
AddcategoryVC
,并在其中创建
BudgetVC编号2
。 因此,调用
BudgetVC编号2
的重新加载数据没有任何作用

使用回调是最常见的解决方案: 使用

移除

let budgetVC = BudgetViewController()
内部
AddCategory

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

didTapAddCategory

let modalViewController = AddCategory()
modalViewController.addCategoryCompletion = { newTransaction in
    self.budgetData.append(newTransaction)
    self.tableView.reloadData()
}
最后在
prin
方法中的dismise action中调用这个完成回调

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)
})

BudgetViewController()
创建一个新实例,该实例不能与呈现当前AddCategory控制器的实例相同。我应该以不同的方式呈现AddCategory控制器?不,但您必须使用回调闭包或协议/委托返回呈现控制器。或者只需使用
presentingViewController
属性在显示AddCategory时使用presentingViewController?当我这样做的时候,我在这条线上遇到了车祸。imageView.frame=CGRect(x:navigationController!.navigationBar.frame.midX-14,y:6,宽:34,高:34)导航控制器!原因crashI做出了这些更改,似乎它仍然没有重新加载表。据我所知,您的表只显示“杂货交易”,而不是从“AddCategoryVC”中选择的类别。原因是我回答中的第一句话,你不能在“AddCategoryVC”中使用“budgetVC.budgetData.append”,因为它是另一个正确的控制器,但在尝试回调代码后,它仍然无法将类别添加到数组并在tableview上显示。我甚至无法将任何数据发送回原始视图控制器。@soRazor我已编辑了原始答案,您可以尝试,现在它应该可以正确处理数据,并且表视图将接收更新。