基本上,我有以下UITableViewController
包含自定义tableView单元格,其中带有标签。选择了单元格后,我希望将单元格的值传递给下一个视图控制器,该控制器将在HTTP POST响应中使用它。
可以添加什么到didSelectRowAt
上,以将所选单元格的值传递给显示的视图控制器?
也许是变量?
以下是我的代码:
import UIKit
class ScheduledCell: UITableViewCell {
@IBOutlet weak var ETALabel: UILabel!
@IBOutlet weak var cellStructure: UIView!
@IBOutlet weak var scheduledLabel: UILabel!
@IBOutlet weak var testingCell: UILabel!
@IBOutlet weak var pickupLabel: UILabel!
@IBOutlet weak var deliveryLabel: UILabel!
@IBOutlet weak var stopLabel: UILabel!
@IBOutlet weak var topBar: UIView!
}
class ToCustomerTableViewController: UITableViewController, UIGestureRecognizerDelegate {
var typeValue = String()
var driverName = UserDefaults.standard.string(forKey: "name")!
var structure = [AlreadyScheduledStructure]()
override func viewDidLoad() {
super.viewDidLoad()
fetchJSON()
//Disable delay in button tap
self.tableView.delaysContentTouches = false
tableView.tableFooterView = UIView()
}
private func fetchJSON() {
guard let url = URL(string: "https://example.com/example/example"),
let value = driverName.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)
else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = "driverName=\(value)".data(using: .utf8)
URLSession.shared.dataTask(with: request) { data, _, error in
guard let data = data else { return }
do {
self.structure = try JSONDecoder().decode([AlreadyScheduledStructure].self,from:data)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
catch {
print(error)
}
}.resume()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return structure.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "scheduledID", for: indexPath) as! ScheduledCell
let portfolio = structure[indexPath.row]
cell.stopLabel.text = "Stop \(portfolio.stop_sequence)"
cell.testingCell.text = portfolio.customer
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let portfolio = structure[indexPath.row]
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "scheduledDelivery")
print(portfolio.customer)
controller.navigationItem.title = navTitle
navigationController?.pushViewController(controller, animated: true)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200.0
}
}
答案 0 :(得分:3)
为要传递给ScheduledDelivery控制器的数据创建公共变量。然后在didselect
委托方法中进行设置。假设您要传递portfolio.customer
。在scheduleDelivery控制器上声明以下公共变量。
public var portfilio:String?
然后像这样通过didselect
方法为该变量设置值,
let portfolio = structure[indexPath.row]
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "scheduledDelivery")
controller.portfilio = portfolio.customer
controller.navigationItem.title = navTitle
navigationController?.pushViewController(controller, animated: true)
答案 1 :(得分:0)
将投资组合变量添加到下一个ViewController
class scheduledDeleivery: UIViewController{
var customer:String? //suposing this is customer type
然后 覆盖func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath){
let portfolio = structure[indexPath.row]
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "scheduledDelivery") as! shcheduledDeleivery
controller.customer = porfolio.customer //here is the customer you need to pass to next viewcontroller
print(portfolio.customer)
controller.navigationItem.title = navTitle
navigationController?.pushViewController(controller, animated: true)
}
答案 2 :(得分:0)
您可以将单元格的数据存储为变量,然后在准备segue函数时将其传递给另一个ViewController。如果您调用此命令是为了准备进行登录,则每次您尝试访问该登录时都会自动执行此操作。
var nameOfVar:String =“”
覆盖功能准备(用于segue:UIStoryboardSegue,发件人:任意?){
var secondVC = segue.destination为! YourSecondViewController
secondVC.variable = nameOfVar
}
希望我有所帮助:)