gpt4 book ai didi

ios - 如何将 TableViewCell 值传递给 Swift 3.0 中的新 ViewController?

转载 作者:行者123 更新时间:2023-11-28 21:13:43 24 4
gpt4 key购买 nike

我有这个 JSON 数据

移动.json

{
"status":"ok",
"movement":
[
{
"refno":"REF 1",
"dtfrom":"2017-13-12"
},
{
"refno":"REF 2",
"dtfrom":"2017-13-13"
},
{
"refno":"REF 3",
"dtfrom":"2017-13-14"
},
]
}

到目前为止,我设法将值提取到 TableViewCell 中。

但我的目标是将值从 ViewController.swift 传递到 MoveDetails.swift 以便该值可以显示在 MoveDetails.swift

我有这四个 swift 文件。我在 ViewController.swift 和 MoveDetails.swift 上遇到了问题。我不确定如何将值传递给新的 Controller 。

代码如下。

ViewController.swift

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
var move: [Move]? = []
override func viewDidLoad() {
super.viewDidLoad()
fetchData()
}
func fetchData() {
let urlRequest = URLRequest(url: URL(string: "http://localhost/move.json")!)
let task = URLSession.shared.dataTask(with: urlRequest) {
(data,response,error)in
if error != nil { return }

self.move = [Move]()
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String: AnyObject]

if let msFromJson = json["movement"] as? [[String: AnyObject]] {
for mFromJson in msFromJson {
let ms = Move()
if let refno = mFromJson["refno"] as? String, let dtfrom = mFromJson["dtfrom"] as? String {
ms.refno = refno
ms.dtfrom = dtfrom
}
self.move?.append(ms)
}
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
}
catch let error{ print(error)}
}
task.resume()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "movementstatusCell", for: indexPath) as! MoveCell

cell.refnoLbl.text = self.move?[indexPath.item].refno
cell.dtfromLbl.text = self.move?[indexPath.item].dtfrom
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.move?.count ?? 0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "MoveDetails") as! MoveDetails

let selectedMove = self.move?[indexPath.item]
vc.refnoString = selectedMove.refno
vc.dtfromString= selectedMove.dtfrom
self.navigationController?.pushViewController(vc, animated: true)
}
}

MoveCell.swift

import UIKit
class MoveCell: UITableViewCell {

@IBOutlet weak var dtfromLbl: UILabel!
@IBOutlet weak var refnoLbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}

移动.swift (NSObject)

import UIKit
class Move: NSObject {
var refno: String?
var dtfrom: String?
}

MoveDetails.swift

import UIKit
class MoveDetails: UIViewController {

@IBOutlet weak var refnoLbl: UILabel!
@IBOutlet weak var dtfromLbl: UILabel!

var refnoString: String!
var dtfromString: String!

override func viewDidLoad() {
super.viewDidLoad()
refnoString = refnoLbl.text
dtfromString = dtfromLbl.text
}
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }
}

感谢有人能提供帮助。谢谢。

最佳答案

您只需设置MoveDetails View Controller 的属性。作为建议

  • 不是在 MoveDetails 中存储 refnoStringdtfromString 属性,您可以只存储一个 Move 类型的属性:
  • 缓存 MoveDetails View Controller 以重用它
  • 实现 viewDidAppear 以更新 MoveDetails socket

所以:

class ViewController: UIViewController, UITableViewDelegate,    UITableViewDataSource {

var detailsVC : MoveDetails?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (detailsVC == nil) {
detailsVC = self.storyboard?.instantiateViewController(withIdentifier: "MoveDetails") as! MoveDetails
}
detailsVC.move = self.move?[indexPath.item]
self.navigationController?.pushViewController(detailsVC , animated: true)
}
}

然后,覆盖 MoveDetails View Controller 中的 viewDidAppear,然后您只需将值填入文本标 checkout 口即可。

class MoveDetails: UIViewController {

@IBOutlet weak var refnoLbl: UILabel!
@IBOutlet weak var dtfromLbl: UILabel!

var move:Move?

override func func viewDidAppear(_ animated: Bool) {
refnoLbl.text = move?.refno
dtfromLbl.text = move?.dtfrom
}
}

语法错误是因为我目前没有可用的 Xcode 来进行检查

关于ios - 如何将 TableViewCell 值传递给 Swift 3.0 中的新 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42220443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com