重新加载TableView调用Firebase Observer后

时间:2017-01-31 05:57:21

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

运行print语句后,我的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()
    }
}

1 个答案:

答案 0 :(得分:0)

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