简体   繁体   English

Swift tableView.reloadData() 在完成处理程序中不工作

[英]Swift tableView.reloadData() not working inside completion handler

I have two view controllers, in controller 1 I have a table view and I am presenting the controller 2 like so:我有两个视图控制器,在 controller 1 我有一个表视图,我正在展示 controller 2,如下所示:

@IBAction func addPOItemButtonPressed(_ sender: Any) {
        
        let itemViewController = self.storyboard?.instantiateViewController(withIdentifier: "LPOsItemController") as! LPOsItemController
        
        itemViewController.RequestID = self.RequestID
        
        let navigationViewController = UINavigationController(rootViewController: itemViewController)
        self.present(navigationViewController, animated: true, completion: nil)
        
    }

When I dismiss controller 2 I am calling a method in controller 1 like so:当我关闭 controller 2 时,我在 controller 1 中调用一个方法,如下所示:

In Controller 1: Controller 1:

NotificationCenter.default.addObserver(self, selector: #selector(self.refreshDetails(notification:)), name:NSNotification.Name(rawValue: "refreshDetails"), object: nil)


@objc func refreshDetails(notification: NSNotification){

        spLPOHeaderGet() { result in

                self.tableView.reloadData()

        }
        
    }

In Controller 2 Controller 2

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refreshDetails"), object: nil)
        
dismiss(animated: true, completion: nil)

My problem is when I call the method in Controller 1, the table view does not reload the data, sometimes it does and sometimes it doesn't.我的问题是当我调用Controller 1中的方法时,表视图没有重新加载数据,有时会,有时不会。 The reload call is in a completion handler.重新加载调用在完成处理程序中。 Can someone tell me what I am doing wrong?有人可以告诉我我做错了什么吗?

Thanks,谢谢,

Posting to notification for this situation is not a good idea.针对这种情况发布通知并不是一个好主意。 You can use the delegate to communicate from ViewController 2 to controller 1.您可以使用委托从 ViewController 2 到 controller 1 进行通信。

if you want to identify an issue in the current implementation try to put a breakpoint on如果您想确定当前实现中的问题,请尝试在

self.tableView.reloadData()

and check this method is called.并检查此方法是否被调用。 and if it is called does tableview is in memory when this called.如果它被调用,那么调用时 tableview 是否在 memory 中。 There is a chance when the completion handler is called this tableView object is deallocted.当调用完成处理程序时,有机会释放此 tableView object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM