如何将选定单元格的值传递给另一个 ViewController?
How pass the value of a selected cell to another ViewController?
基本上,我有以下 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
}
}
将投资组合变量添加到您的下一个 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)
}
您可以将单元格的数据存储为变量,然后在准备 segue 函数时将其传递给另一个 ViewController。如果你在准备 segue 时调用它,它会在你每次尝试访问该 segue 时自动执行。
var nameOfVar : String = ""
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var secondVC = segue.destination as!你的第二个ViewController
secondVC.variable = nameOfVar
}
希望我帮到了你:)
为要传递给 scheduledDelivery 的数据创建 public 变量 controller.Then 在 didselect
委托方法中设置它们。假设你想通过 portfolio.customer
。在 scheduledDelivery 控制器上声明以下 public 变量。
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)
基本上,我有以下 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
}
}
将投资组合变量添加到您的下一个 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)
}
您可以将单元格的数据存储为变量,然后在准备 segue 函数时将其传递给另一个 ViewController。如果你在准备 segue 时调用它,它会在你每次尝试访问该 segue 时自动执行。
var nameOfVar : String = ""
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var secondVC = segue.destination as!你的第二个ViewController
secondVC.variable = nameOfVar
}
希望我帮到了你:)
为要传递给 scheduledDelivery 的数据创建 public 变量 controller.Then 在 didselect
委托方法中设置它们。假设你想通过 portfolio.customer
。在 scheduledDelivery 控制器上声明以下 public 变量。
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)