刷新UITableView时调用两次函数

时间:2017-10-10 22:20:50

标签: ios swift uitableview nsnotificationcenter

我有一个UIPageViewController和一个UITableViewController。我更新我的模型并在UIPageViewController中调用UITableViewController。我的问题是我想在下拉时刷新UITableView。下面的代码有效,但不会停止刷新并调用刷新功能两次。

的UITableViewController

    override func viewDidLoad() {
    // Table View Settings.
   if #available(iOS 10.0, *) {
        tableView.refreshControl = UIRefreshControl()
    } else {
        // Fallback on earlier versions
    }
    if #available(iOS 10.0, *) {
        if(section == 0){
            tableView.refreshControl?.addTarget(Model.sharedInstance, action: #selector(Model.refresh_fiks), for: .valueChanged)
        }else{
            tableView.refreshControl?.addTarget(Model.sharedInstance, action: #selector(Model.refresh_fiks2), for: .valueChanged)
        }
    } else {
        // Fallback on earlier versions
    }
    NotificationCenter.default.addObserver(self, selector: #selector(TableViewController.refreshTable(_:)), name: NSNotification.Name(rawValue: "refresh"), object: nil)
}
func refreshTable(_ notification: Notification) {
    guard let temp_model = notification.userInfo?["passed_data"] as? [Team] else{
    return
    }
    print(temp_model[0].name)
    model = temp_model
    tableView.reloadData()
}

UIPageViewController

 func modelUpdated() {
    let tvc = storyboard?.instantiateViewController(withIdentifier: viewControllerIdentifier) as! TableViewController
        // Install the first-presented view controller
        tvc.section = 0
    print(model.teams)
        tvc.model = model.teams
    if (flag == true) {
        let userInfo = [ "passed_data" : model.teams ]
        NotificationCenter.default.post(name: Notification.Name(rawValue: "refresh"), object: nil, userInfo: userInfo)
    }else{
        setViewControllers([tvc], direction: .forward, animated: true, completion: nil)
    }
}

0 个答案:

没有答案