我希望将Firebase数据库中的数据加载到主视图控制器中的表视图中,然后将该数据传递给作为详细视图控制器的第二个视图控制器,使其更具描述性。我被卡住了,因为当我点击单元格时,它不会跳转到我的另一个视图控制器,我不知道如何传递数据。我只想在第一个视图控制器上输入名称,然后在详细视图控制器上填充其他数据。
我的主视图控制器——
import UIKit
import Foundation
import Firebase
import FirebaseDatabase
class ViewController: UIViewController {
var table = [FacStaffInfo]()
var ref: DatabaseReference!
@IBOutlet weak var Tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
ref = Database.database().reference().child("users")
ref.observe(DataEventType.value, with: {(snapshot) in
if snapshot.childrenCount > 0 {
self.table.removeAll()
for user in snapshot.children.allObjects as! [DataSnapshot] {
let object = user.value as? [String: AnyObject]
let title = object?["title"]
let name = object?["name"]
let email = object?["email"]
let phone = object?["phone"]
let office = object?["office"]
let bio = object?["bio"]
let user = FacStaffInfo(title: title as! String, name: name as! String, email: email as! String, phone: phone as! Int, office: office as! String, bio: bio as! String)
self.table.append(user)
self.Tableview.reloadData()
}
}
})
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return table.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "userCell") as! TableViewCell
let user: FacStaffInfo
user = table[indexPath.row]
cell.titleLabel?.text = user.title
cell.nameLabel?.text = user.name
cell.emailLabel?.text = user.email
cell.phoneLabel?.text = String(user.phone)
cell.officeLabel?.text = user.office
cell.bioLabel?.text = user.bio
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = Tableview.indexPathForSelectedRow {
let destinationController = segue.destination as! InfoViewController
destinationController.FacStaffData = [table[indexPath.row]]
}
}
}
}
我的详细视图控制器——
import UIKit
import Firebase
import FirebaseDatabase
class InfoViewController: UIViewController {
var FacStaffData = [FacStaffInfo]()
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var emailLabel: UILabel!
@IBOutlet weak var phoneLabel: UILabel!
@IBOutlet weak var officeLabel: UILabel!
@IBOutlet weak var bioLabel: UILabel!
//var title = ""
var name = ""
var email = ""
var phone = ""
var office = ""
var bio = ""
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = title
nameLabel.text = name
emailLabel.text = email
phoneLabel.text = phone
officeLabel.text = office
bioLabel.text = bio
print(titleLabel)
}
}
我的信息类——
import Foundation
import Firebase
import FirebaseDatabase
class FacStaffInfo {
var title: String
var name: String
var email: String
var phone: Int
var office: String
var bio: String
init(title: String, name: String, email: String, phone: Int, office: String, bio: String) {
self.title = title;
self.name = name;
self.email = email;
self.phone = phone;
self.office = office;
self.bio = bio
}
}
和我的表视图单元格
import UIKit
import Firebase
import FirebaseDatabase
class TableViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var emailLabel: UILabel!
@IBOutlet weak var phoneLabel: UILabel!
@IBOutlet weak var officeLabel: UILabel!
@IBOutlet weak var bioLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
看起来didSelectRowAt只是在不调用任何东西的情况下再次实现了didSeleceRowAt。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: self)
}
}
重试:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: self)
}
更新以进行跟进:
InfoViewController使用默认值。当您准备segue时,您将信息放入FacStaffData中。这应该奏效。
class InfoViewController: UIViewController {
var FacStaffData = [FacStaffInfo]()
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var emailLabel: UILabel!
@IBOutlet weak var phoneLabel: UILabel!
@IBOutlet weak var officeLabel: UILabel!
@IBOutlet weak var bioLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = FacStaffData.title
nameLabel.text = FacStaffData.name
emailLabel.text = FacStaffData.email
phoneLabel.text = FacStaffData.phone
officeLabel.text = FacStaffData.office
bioLabel.text = FacStaffData.bio
print(titleLabel)
}
}