0
こんにちは、私は実際には、別のビューコントローラにテーブルビューからデータを渡そうとしている問題があります。私はすべてのユーザのリストを表示するテーブルビューを持っていますが、ユーザをクリックすると、その特定のユーザのデータを表示せずに次のビューコントローラに移動します。以下のコードは、私が従った例とほとんど同じですが、何らかの理由で私のために働いていません。助けてください、私は解決策を探してみましたが、何も見つかりませんでした。また、Firebaseを使用してデータを保存したり取得したりしています。事前のおかげでテーブルビューのセルとビューコントローラの間でfirebaseからデータを渡す方法は?
@IBOutlet weak var searchTableView: UITableView!
var profiles = [user]()
var ref: FIRDatabaseReference!
override func viewdidload(){
databaseRef = FIRDatabase.database().reference()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "guestSegue", sender: self)
self.searchTableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "guestSegue" {
if let indexpath = searchTableView.indexPathForSelectedRow {
let guestVC = segue.destination as! guestProfileViewController
guestVC.ref = profiles[indexpath.row].ref
}
}
}
ユーザアレイ
class User {
var backgroundProfileImage: String!
var fullName: String!
var profilePic: String!
var ref: FIRDatabaseReference!
var key: String
init(backgroundProfileImage: String, fullName: String, profilePic: String, key: String = ""){
self.backgroundProfileImage = backgroundProfileImage
self.fullName = fullName
self.profilePic = profilePic
self.key = key
self.ref = FIRDatabase.database().reference()
}
init(snapshot: FIRDataSnapshot){
self.backgroundProfileImage = value["backgroundProfileImage"] as! String!
self.fullName = (snapshot.value as! NSDictionary)["fullName"] as! String
self.profilePic = (snapshot.value as! NSDictionary)["profilePic"] as! String
self.uid = (snapshot.value as! NSDictionary)["uid"] as! String!
self.key = snapshot.key
self.ref = snapshot.ref
}
guestViewController
class guestProfileViewController: UIViewController, UINavigationControllerDelegate {
@IBOutlet weak var profileImage: UIImage!
@IBOutlet weak var backgroundProfileImage: UIImageView!
@IBOutlet weak var fullNameLabel: UILabel!
var ref: FIRDatabaseReference?
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
loadProfileData()
}
func loadProfileData(){
if let ref = ref {
ref.observe(.value, with: { (user) in
let user: User = User(snapshot: user)
self.fullNameLabel.text = user.fullName
self.profileImage.sd_setImage(with: URL(string: user.profilePic), placeholderImage: UIImage(named: "default"))
self.backgroundProfileImage.sd_setImage(with: URL(string: user.backgroundProfileImage), placeholderImage: UIImage(named: "default_1"))
})
}
}
}
guestProfileViewControllerのviewDidLoad()に 'ref'が設定されています。 'ref'をユーザの' ref'と等しくしたいと思うように、そのビットを削除してみてください。 –
ありがとうございました!出来た! @PeterTao –
素晴らしい!私は答えとして投稿しました:) –