【问题标题】:Reloading TableView After calling Firebase Observer调用 Firebase Observer 后重新加载 TableView
【发布时间】:2017-01-31 05:57:20
【问题描述】:

在运行打印语句后,我的 cellForRowAtIndexPath 没有被调用。发现我的 tableView 没有重新加载...尝试了几个地方,但仍然没有重新加载。有人知道为什么吗?`

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self


    DataService.ds.DATABASE_REF_USERS.child(CURRENTUSER!).child("cart").observe(.value, with: { (snapshot) in

        print("check")
        if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {

            self.cartItems = []

            for snap in snapshot {

                let shoeId = snap.key

                DataService.ds.DATABASE_REF_SHOES.child("upcomingShoes").child(shoeId).observeSingleEvent(of: .value, with: { (snapshot) in

                    if let shoeData = snapshot.value as? Dictionary<String, AnyObject> {
                        let shoe = Shoe(shoeId: shoeId, shoeData: shoeData)
                        print(shoe.shoeName)
                        self.cartItems.append(shoe)

                    }
                })
            }
        }
        self.tableView.reloadData()
    })
    print(self.cartItems.count)
}

//添加到我的数据库:

    @IBAction func addToCartPressed(_ sender: Any) {

            //Under the current users, child node called "cart", I want to add the current shoes id

            let currentUserCartRef = DataService.ds.DATABASE_REF_USERS.child(CURRENTUSER!).child("cart")

            let shoeDataToAdd = [shoe.shoeId: true]

            currentUserCartRef.updateChildValues(shoeDataToAdd)


        }

`

表格视图单元格方法

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cartItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let shoe = cartItems[indexPath.row]

    if let cell = tableView.dequeueReusableCell(withIdentifier: "cartCell", for: indexPath) as? CartCell {

        if let shoeImage = MainVC.imageCache.object(forKey: shoe.shoeImageUrl as NSString) {

            cell.configureCell(shoe: shoe, shoeImage: shoeImage)
        }
        else {

            cell.configureCell(shoe:shoe)
        }
        return cell

    }
    else {
        return PreviewCell()
    }
}

【问题讨论】:

  • 我的班级名列前茅:var cartItems = [Shoe]()
  • 该打印语句在执行self.tableView.reloadData() 行之前执行。所以很明显它打印0作为输出。您还需要在主线程上重新加载 tableView。
  • 嘿,我只是在尝试不同的区域,看看代码是否正在运行,打印语句是否在那里运行,或者 self.tableView.reloadData() 是否在哪里运行
  • @NiravD 如何在主线程上调用 reload?
  • 应该是DispatchQueue.main.async { self.tableView.reloadData() }

标签: ios swift uitableview firebase firebase-realtime-database


【解决方案1】:

你可以检查你的 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { } 可能它为您的表格视图单元格返回 0 行。您必须返回大于 0 的行数才能重新加载表格视图单元格。

【讨论】:

  • 嘿,我更新了代码以显示我的 tableView 方法。我遇到的问题是它没有重新加载数据。我可以通过在主线程上运行它来修复它,现在我在更新数据库时无法重新加载数据
  • 更新数据库后,您是否检查过cartItem 数组在表格视图委托方法(numberOfRows 和cellForIndexPath)中有有效数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 2017-11-14
  • 1970-01-01
相关资源
最近更新 更多