如何修复准备数据时的延迟数据?

时间:2019-05-28 08:34:53

标签: swift prepared-statement segue

当我想在表视图中传递数据并将其传递给另一个viewcontroller时,数据延迟了1次

我正在准备进行搜索。

现在,为了获取正确的数据,我需要返回表格视图并按同一行

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toRequestCorrection"{
            let destinationVC = segue.destination as! RequestCorrectionViewController
            if let indexPath = tableView.indexPathForSelectedRow{
                destinationVC.Shift = self.correction["Shift"].stringValue
                destinationVC.LogDate = self.correction["LogDate"].stringValue
                destinationVC.logIn = self.correction["ActualLogIn"].stringValue
                destinationVC.logOut = self.correction["ActualLogOut"].stringValue
                destinationVC.breakEnd = self.correction["ActualBreakEnd"].stringValue
                destinationVC.breakStart = self.correction["ActualBreakStart"].stringValue
                destinationVC.overTimeIn = self.correction["ActualOverTimeIn"].stringValue
                destinationVC.overTimeOut = self.correction["ActualOverTimeOut"].stringValue
                destinationVC.transactionStatusID = self.correction["TransactionStatusID"].intValue
            }
        }
    }

它应该在我按下该行之后立即在该行上传递数据

2 个答案:

答案 0 :(得分:0)

您应该在viewController中实现tableVew(_:willSelectRowAt:)的{​​{1}}方法,并将更正属性设置为所选行的值。

UITableViewDelegate

还请注意,您可以在class YourSourceViewController: UITableViewDelegate{ var correction: Correction! func tableView(_ tabelView: UITableView, willSelecRowAt indexPath: IndexPath) -> IndexPath?{ correction = correctionList[indexPath.row] return indexPath } func prepare(for segue: UIStoryBoardSegue, sender: Any?){ //passing data to RequestCorrectionViewController. //And also you don't need to check if indexPath is nil. //Because this block will called only any row of tableView is selected. } } 方法中执行相同的操作,但是此方法在执行segue之后会起作用,并且也会引起您遇到的问题。

修改

如果您认为使用tableView(_:didSelectRowAt:)方法是一种误用,则可以在您的StoryController上设置segue的源(而不是模板单元),并在Storyboard上设置标识符并以编程方式调用它。就像@vadian的说

willSelectRowAt:

答案 1 :(得分:0)

此外,您不必使用segue,可以使用以下代码在didSelectRowAt方法中实例化视图控制器,而无需prepareForSegue。

编辑:您没有表示这是一个异步任务。所以,我在完成处理程序中使用Alamofire。我认为这对您有用。

typealias yourCompletionHandler = (Data?, _ message: String?, _ errorStatusCode: Int?) -> Void
func yourAsyncTask(handler: @escaping yourCompletionHandler) {
    let parameters: [String: Any] = ["unitId": 4124]
    let url = ""
    Alamofire.request(url, method: .get, parameters: parameters)
        .responseObject { (response: DataResponse<BaseListResponseParser<YourModel>>) in
            switch response.result {
            case .success:
                if let result = response.result.value {
                  handler(result.listItems, nil, nil)
                }
            case .failure(let error):
                print(error)
            }
    }
} 


yourAsyncTask { [weak self] (yourModel, error, _) in
        DispatchQueue.main.async {
            guard let destination = UIStoryboard(name: "Main", bundle: nil)
                .instantiateViewController(withIdentifier: "ViewControllerId") as? RequestCorrectionViewController else { return }
            destination.Shift = yourModel.shift
            navigationController?.pushViewController(destination, animated: true)
        }
    }

使用这种方式,您无需创建segue或prepareForSegue方法。