Tableview没有在xcode 9.3中重新加载数据

时间:2018-04-26 13:41:07

标签: ios swift uitableview firebase

当我致电self.tableview.reloadData()时,它无法正常工作。这段代码只有一次工作。我搜索了很多但找不到解决方案。

import UIKit
import FirebaseStorage
import FirebaseDatabase
import FirebaseAuth
import FirebaseCore
import SDWebImage

class bags : UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var postCommentArray = [String]()
    var useremailArray = [String]()
    var postImageURLArray = [String]()


    override func viewDidLoad() {
        super.viewDidLoad()

        getDataFromServer()

    }

    func getDataFromServer() {
        Database.database().reference().child("users").observe(DataEventType.childAdded) { (snapshot) in

            let values = snapshot.value! as! NSDictionary

            let post = values["post"] as! NSDictionary

            let postIDs = post.allKeys

            for id in postIDs {

                let singlePost = post[id] as! NSDictionary

                self.useremailArray.append(singlePost["postedby"] as! String)
                self.postCommentArray.append(singlePost["posttext"] as! String)
                self.postImageURLArray.append(singlePost["image"] as! String)

            }

                self.tableView.reloadData()

        }

    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableView

        cell.postImage.sd_setImage(with: URL(string: self.postImageURLArray[indexPath.row]))
        cell.tarhanaIlk.text = postCommentArray[indexPath.row]
        return cell
    }

}

**这是tableview单元格代码.3天前工作但现在无法正常工作。如果需要podfile我可以分享。我也试过DispatchQueue.main.async {                 self.tableView.reloadData()             } **在这里输入代码

import UIKit

class TableView: UITableViewCell {

    @IBOutlet weak var tarhanasonLabel: UILabel!
    @IBOutlet weak var postImage: UIImageView!
    @IBOutlet weak var tarhanaIlk: UITextView!


    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
    }

}

这部分是将数据添加到我的火力库存储

导入基金会 导入UIKit 导入FirebaseStorage 导入FirebaseDatabase 导入FirebaseAuth 导入FirebaseCore

类文件:UIViewController {

var baslangicDegeri = 0
var iskembeadet = 0
var mercimekadet = 0
var ezogelinadet = 0
var domatesadet = 0
var sehriyeadet = 0
var etliturluadet = 0
var hamburgeradet = 0
var kanatadet = 0
var pizzaadet = 0
var etsoteadet = 0
var alabalikadet = 0
var uuid = NSUUID().uuidString



@IBOutlet weak var tarhanaadetlabel: UILabel!
@IBOutlet weak var iskembeadetlabel: UILabel!
@IBOutlet weak var mercimekadetlabel: UILabel!
@IBOutlet weak var ezogelinadetlabel: UILabel!
@IBOutlet weak var domatesadetlabel: UILabel!
@IBOutlet weak var sehriyeadetlabel: UILabel!



@IBOutlet weak var etliturluadetlabel: UILabel!
@IBOutlet weak var hamburgeradetlabel: UILabel!
@IBOutlet weak var kanatadetlabel: UILabel!
@IBOutlet weak var pizzaadetlabel: UILabel!
@IBOutlet weak var etsoteadetlabel: UILabel!
@IBOutlet weak var alabalikadetlabel: UILabel!



@IBOutlet weak var postImage: UIImageView!
@IBOutlet weak var postImageiskembe: UIImageView!
@IBOutlet weak var postImagemercimek: UIImageView!
@IBOutlet weak var postImageezogelin: UIImageView!
@IBOutlet weak var postImagedomates: UIImageView!
@IBOutlet weak var postImagesehriye: UIImageView!



@IBOutlet weak var postImageetliturlu: UIImageView!
@IBOutlet weak var postImagehamburger: UIImageView!
@IBOutlet weak var postImagekanat: UIImageView!
@IBOutlet weak var postImagepizza: UIImageView!
@IBOutlet weak var postImageetsote: UIImageView!
@IBOutlet weak var postImagealabalik: UIImageView!







override func viewDidLoad() {

    super.viewDidLoad()
}



@IBAction func tarhanaEkle(_ sender: Any) {


        let mediaFolder = Storage.storage().reference().child("media")

        if let data = UIImageJPEGRepresentation(postImage.image!, 0.5) {

            mediaFolder.child("\(uuid).jpg").putData(data, metadata: nil, completion: { (metadata, error) in
                if error != nil {
                    let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
                    let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
                    alert.addAction(okButton)
                    self.present(alert, animated: true, completion: nil)
                } else {

                    let imageURL = metadata?.downloadURL()?.absoluteString

                    let post = ["image" : imageURL!, "postedby" : Auth.auth().currentUser!.email!, "uuid" : self.uuid, "posttext" : self.tarhanaadetlabel.text as Any] as [String : Any]
                    Database.database().reference().child("users").child((Auth.auth().currentUser?.uid)!).child("post").childByAutoId().setValue(post)

                    self.postImage.image = UIImage(named: "select.png")
                    self.tarhanaadetlabel.text = ""
                    self.tabBarController?.selectedIndex = 0

                }



            })
        }



    }





@IBAction func eksibuton(_ sender: Any) {
    if (baslangicDegeri > 0 ) {
        baslangicDegeri -= 1
        tarhanaadetlabel.text = String(baslangicDegeri)
        tarhanaadetlabel.text = "ADET : \(baslangicDegeri) "
    }
}


@IBAction func artibuton(_ sender: Any) {

    baslangicDegeri += 1
    tarhanaadetlabel.text = String(baslangicDegeri)
    tarhanaadetlabel.text = "ADET : \(baslangicDegeri) "
    }

1 个答案:

答案 0 :(得分:1)

我的猜测是你的Firebase childAdded函数的完成处理程序是在后台线程上调用的。您需要在主线程上发出reloadData调用。尝试使用dispatchAsync:

DispatchQueue.main.async {
            self.tableView.reloadData()
}