我正在制作一个项目,它具有firebase数据库和swift。我有它,以便用户可以发布帖子(它是我的个人应用程序,主要是一个有趣的项目),并且可以上传到表格视图中。但是在表格视图单元格中,我有一个标签,名称应该在此放置。它是一个自定义表格视图单元格。我也有正确的设置。
问题出在当我尝试将作者姓名放入标签单元中时。我已经尝试了很多东西...但我无法弄清楚。
我的代码中有两个类。我知道这可能不是最佳的编码实践,我的代码也可能不是最佳的-但我正在学习。
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
let userID = Auth.auth().currentUser?.uid
var currentuser:String = ""
class TweetViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var authornamereference: String = ""
var timelinepost:[String] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return timelinepost.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TweetTableViewCell
cell.postcontenttext.text = self.timelinepost[indexPath.row]
cell.postauthorlabel.text = self.authornamereference
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
@IBOutlet weak var mytableview: UITableView!
func alertcreated (title:String, message:String){
let alert = UIAlertController(title: title, message: message, preferredStyle:.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
@IBOutlet weak var NameLabel: UILabel!
var ref:DatabaseReference!
let userID = Auth.auth().currentUser?.uid
var possstkeystring:String = "author name"
override func viewDidLoad() {
super.viewDidLoad()
possstkeystring = "author"
ref = Database.database().reference()
ref.child("User").child(userID!).observe(.value) { (snapshot) in
let userinfo = snapshot.value as? NSDictionary
let username = userinfo!["Name"] as! String
self.NameLabel.text = username
self.NameLabel.font = UIFont(name: "Bodoni 72 Smallcaps", size: 25)
currentuser = self.NameLabel.text!
}
self.ref.child("All Post").observe(DataEventType.childAdded) { (snapshot) in
if let item = snapshot.value as? NSDictionary{
var keyvalues: [String:Any] = [:]
keyvalues = ["Post Content": item["Post Content"] as Any]
self.timelinepost.append(keyvalues["Post Content"] as! String)
self.mytableview.reloadData()
}
self.ref.child("All Post").child(self.possstkeystring).child("Post Author").observe(DataEventType.value, with: { (snapshot) in
if let item = snapshot.value as? NSDictionary{
self.authornamereference = item["Post Author"] as! String
}
})
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
@IBAction func addtweet(_ sender: UIButton) {
performSegue(withIdentifier: "addnewpostsegue", sender: self)
}
}
class TestAddPost: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
}
@IBOutlet weak var postbuttonoutlet: UIButton!
@IBAction func cancelbacktotimeline(_ sender: UIButton) {
self.performSegue(withIdentifier: "cancelpost", sender: self)
}
var officialpostkeyvalue = ""
@IBAction func postbuttonaddpost(_ sender: UIButton) {
var ref: DatabaseReference!
ref = Database.database().reference()
let postvalue = ref.child("All Post").childByAutoId()
let postkeyvalue = postvalue.key!
postvalue.child("Post Content").setValue(postcontent.text)
postvalue.child("Post Author").setValue(currentuser)
officialpostkeyvalue = postkeyvalue
self.performSegue(withIdentifier: "addpostbacktotimeline", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let tabCtrl = segue.destination as! UITabBarController
let tweetviewcontroller = tabCtrl.viewControllers![0] as! TweetViewController
tweetviewcontroller.possstkeystring = officialpostkeyvalue
}
@IBOutlet weak var postcontent: UITextView!
}
我知道我的变量名不是传统的,或者我编程的方式可能不是最好的,但是我想用它来工作,只是作者名不起作用。
我如何设置它:
当用户单击发布->时,它将使用我拥有的名称标签并将其存储为作者。.然后,它以树格式存储在firebase数据库中 “所有帖子”->随机ID->作者姓名 “所有帖子”->随机ID->帖子内容
然后,我尝试检索作者姓名并将其放在标签中,而我没有做任何工作。请帮忙!