gpt4 book ai didi

ios - 在 Swift 中的 TableView 和另一个 ViewController 之间传递数据

转载 作者:搜寻专家 更新时间:2023-11-01 06:16:12 25 4
gpt4 key购买 nike

我试图在 tableView 中显示一个事件列表,当用户按下其中一个事件时,它将打开另一个 viewController,其中包含有关该特定选择的更多信息。

这是具有可以选择的选项的tableView:

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase

class NewsfeedViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var ref:DatabaseReference!,
posts = [eventStruct]()
@IBOutlet weak var tableview: UITableView!
var propertStruct : (Any)? = nil


override func viewDidLoad() {
super.viewDidLoad()
loadNews()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func loadNews() {
ref = Database.database().reference()
ref.child("events").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in

if let valueDictionary = snapshot.value as? [AnyHashable:String]
{
let title = valueDictionary["Title"]
let location = valueDictionary["Location"]
let date = valueDictionary["Date"]
let description = valueDictionary["Description"]
self.posts.insert(eventStruct(title: title, date: date, location: location, description: description), at: 0)
self.tableview.reloadData()
}
})

}

///////////////////////// Table View Content \\\\\\\\\\\\\\\\\\\\\\\\\\
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let label1 = cell.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].title
let label2 = cell.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].location
let label3 = cell.viewWithTag(3) as! UILabel
label3.text = posts[indexPath.row].date
return cell
}

func prepareForSegue(segue: UIStoryboardSegue, sender: eventStruct) {
let secondViewController = segue.destination as? EventViewController
secondViewController?.data = sender
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetails", sender: posts[indexPath.row])
}
/////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
}

struct eventStruct {
let title: String!
let date: String!
let location: String!
let description: String!


}

这里是 DetailsViewController,它应该显示在前面的 tableView 中选择的不同选项的标题:

import UIKit

class EventViewController: UIViewController {

var data: eventStruct?
@IBOutlet weak var titleLabel: UILabel!


override func viewDidLoad() {
super.viewDidLoad()
print (data?.title as Any)
self.titleLabel.text = self.data?.title

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

问题在于,不是显示应该存储在结构变量中的标题,而是始终返回一个 nil 值。

有人可以告诉我我做错了什么吗?我已尝试遵循所有提出的建议,但没有任何效果。

最佳答案

您的 prepareForSegue 必须覆盖 UIViewController 方法,即

func prepare(for segue: UIStoryboardSegue, sender: Any?)

因此您在 NewsfeedViewController 中的方法应该是

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondViewController = segue.destination as? EventViewController
secondViewController?.data = sender as? eventStruct
}

添加一些错误检查也是个好主意

关于ios - 在 Swift 中的 TableView 和另一个 ViewController 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44562178/

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