完成或删除操作后,tableview中的第一行不会消失

时间:2019-05-12 22:33:30

标签: swift uitableview

我有一个tableView,当用户添加记录时填充它。此数据存储在实时Firebase中。向右滑动允许用户完成任务,并将记录从修复子项移到完整的子项。向左滑动可让用户删除该行。在这两种情况下,都将从表中删除该行。除第一行外,这对所有行均适用。当用户执行上述任一操作时,实时数据库将正确执行功能;但是,第一行保留数据。我想念什么?

override func viewDidLoad() {
    super.viewDidLoad()
    self.doListTableView.delegate = self
    self.doListTableView.dataSource = self


    //implement Google ADMobs
            // In this case, we instantiate the banner with desired ad size.
            bannerView = GADBannerView(adSize: kGADAdSizeBanner)

            addBannerViewToView(bannerView)
            bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
            bannerView.rootViewController = self
            bannerView.load(GADRequest())
            bannerView.delegate = self

    //get reference to firebase database
    refRepairs = Database.database().reference().child("repairs").child(UserID!);

        //observing the data changes
        refRepairs!.observe(DataEventType.value, with: { (snapshot) in
        //if the reference have some values
        if snapshot.childrenCount > 0 {

            //clearing the list
            self.repairList.removeAll()

            //iterating through all the values
            for repairs in snapshot.children.allObjects as! [DataSnapshot] {
                //getting values
                let repairObject = repairs.value as? [String: AnyObject]
                let brand  = repairObject?["brand"]
                let id  = repairObject?["id"]
                let categories = repairObject?["categories"]
                let modelNumber = repairObject?["modelNumber"]

                //creating artist object with model and fetched values
                let repair = RepairModel(id: id as! String?, categories: categories as! String?, brand: brand as! String?, modelNumber: modelNumber as! String?)

                self.repairList.append(repair)


            }
            //reloading the tableview
            self.doListTableView.reloadData()
        }
    })
}

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let complete = UIContextualAction.init(style: .normal, title: "Complete") { (action, view, completion) in


                let repairList = self.repairList[indexPath.row]

                if let selectedRepairItem = repairList.id {

                    Database.database().reference().child("completed").child(self.UserID!).child(selectedRepairItem).setValue(["id": repairList.id, "brand": repairList.brand, "categories": repairList.categories, "modelNumber": repairList.modelNumber])
                    Database.database().reference().child("repairs").child(self.UserID!).child(selectedRepairItem).removeValue()

                }

                self.doListTableView.reloadData()


        }
                //        complete.image = //... Add an image
                complete.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
                let action = UISwipeActionsConfiguration.init(actions: [complete])
                action.performsFirstActionWithFullSwipe = true //... Full swipe support
                return action
}

     func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

    let repairList = self.repairList[indexPath.row]
    if let selectedRepairItem = repairList.id {
        Database.database().reference().child("repairs").child(UserID!).child(selectedRepairItem).removeValue()
        self.doListTableView.reloadData()

    }
}

我的工作非常顺利,但退出时遇到了问题。我已解决此问题,但现在我必须解决这个问题。不知道为什么会这样。预先感谢。

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

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

    repair = repairList[indexPath.row]

    cell.labelBrand.text = repair.brand
    cell.labelModelNumber.text = repair.modelNumber

    return(cell)
}

这是我的模特:

class RepairModel {

var id: String?
var categories: String?
var brand: String?
var modelNumber: String?

init(id: String?, categories: String?, brand: String?, modelNumber: String?){
    self.id = id
    self.categories = categories
    self.brand = brand
    self.modelNumber = modelNumber
}
}

0 个答案:

没有答案