首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修正延迟的数据准备?

如何修正延迟的数据准备?
EN

Stack Overflow用户
提问于 2019-05-28 08:34:53
回答 2查看 225关注 0票数 0

当我想在我的表视图中传递数据并将它传递给另一个视图控制器时,我的数据被延迟了一次。

我用的是准备准备。

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

代码语言:javascript
复制
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
            }
        }
    }

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-28 13:22:54

此外,您不必使用segue,您可以在didSelectRowAt方法中实例化视图控制器,并在没有prepareForSegue的情况下使用下面的代码。

编辑:您没有指出这是一个异步任务。所以,我使用的是Alamofire和完成处理程序。我想这对你会有用的。

代码语言:javascript
复制
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方法。

票数 0
EN

Stack Overflow用户

发布于 2019-05-28 12:56:48

您应该在您的tableVew(_:willSelectRowAt:)中实现UITableViewDelegateUITableViewDelegate方法,并将更正属性设置为选定行的值。

代码语言:javascript
复制
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.
    }
}

还请注意,您可以在tableView(_:didSelectRowAt:)方法中执行相同的操作,但是此方法在执行segues之后可以工作,并且也会导致遇到的问题。

编辑

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

代码语言:javascript
复制
func tableView(_ tabelView: UITableView, didSelecRowAt indexPath: IndexPath){
        correction = correctionList[indexPath.row]
        performSegue(withIdentifier "mySegueIdentifier", sender: self)
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56338107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档