如何将选定单元格的值传递给另一个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
    }

}
jo

在下一个ViewController中添加一个Portfolio变量

   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)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

快速获取另一个ViewController中选定单元格的数据(以编程方式)

将数据框中的选定单元格值替换为另一个单独数据框中的值

iOS如何将选定的单元格值隔离到另一个视图控制器中

如何将一个单元格的值更改为另一个单元格

如何将一个单元格的值更改为另一个单元格的不同值?

如何将文本从单元格传递到另一个View Controller中的textView?

如何使用函数将一个单元格值插入另一个单元格?

将 UITableViewController 中选定单元格的“正确”数据传递给 ViewController

将单元格值从html表传递到另一个页面

每次使用宏如何将当前单元格的值与另一个单元格的值求和?

如何将基于多个单元格条件的值复制到另一个工作表

如何使用单个变量将选定的imageview值传递给另一个活动?

如何将一个函数的值传递给另一个函数?

如何将动态生成的单元格的内容与另一个单元格锁定

如何根据另一个单元格将单元格值限制为最大值?

如何将标头(或单元格)的字符串连接到另一个单元格的值?

excel:如何将公式“克隆”到另一个单元格?

从另一个工作表上的特定单元格中搜索值

将特定单元格数据移动到另一个单元格

在范围内的特定单元格中输入的条件值,将解锁另一个范围内的另一个特定单元格(VBA,范围偏移)

将特定单元格复制到另一个工作表

如何让Excel根据另一个数据表将值粘贴到特定单元格中

如何根据值excel VBA将多行复制到特定单元格中的另一个工作表

如何将选定的日期(值)从反应日历传递到另一个组件?

如何将一个单元格的数据自动添加到另一张纸上的另一个单元格?

当单元格中按下按钮时,如何将数据从一个Tableview传递到另一个Tableview?

如何将选定的单元格数组发送到Objective-C中的另一个视图?

VBA如何显示与另一个单元格相邻的单元格的值?

更改另一个单元格时清除特定单元格