当我想在我的表视图中传递数据并将它传递给另一个视图控制器时,我的数据被延迟了一次。
我用的是准备准备。
现在,为了获得正确的数据,我需要返回到表视图并按下同一行。
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
}
}
}它应该在我按下行之后传递行上的数据
发布于 2019-05-28 13:22:54
此外,您不必使用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方法。
发布于 2019-05-28 12:56:48
您应该在您的tableVew(_:willSelectRowAt:)中实现UITableViewDelegate的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.
}
}还请注意,您可以在tableView(_:didSelectRowAt:)方法中执行相同的操作,但是此方法在执行segues之后可以工作,并且也会导致遇到的问题。
编辑
如果您认为使用willSelectRowAt:方法是一种误用,可以将segue的源设置为viewController (而不是模板单元格),并在Storyboard上设置标识符,并以编程方式调用它。就像@vadian说的
func tableView(_ tabelView: UITableView, didSelecRowAt indexPath: IndexPath){
correction = correctionList[indexPath.row]
performSegue(withIdentifier "mySegueIdentifier", sender: self)
}https://stackoverflow.com/questions/56338107
复制相似问题