保存新数据

时间:2018-01-18 19:41:50

标签: swift xcode firebase firebase-realtime-database tableview

首先抱歉我的英语不好。

我的问题是,只要我在Firebase中保存新文件,我的tableview就会被误传。

保存时,会调用一个新的AddViewController。 保存后会自动返回TableviewController。 但是,如果我停止并重新启动应用程序以重新创建TableviewController,我的tableview将正确显示。

我从数组创建一个标头。 然后我从另一个数组创建单元格。

我不明白为什么在保存后返回时我的tableview显示不正确,但是当我在应用程序完成后重新创建视图时,所有内容都显示正确。

我的示例图片:

那是对的:

right

那是坏事

bad

TableViewController中的我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refresher.addTarget(self, action: #selector(TagebuchViewController.refresh), for: UIControlEvents.valueChanged)
    tableView.addSubview(refresher)
    loadTableview()
}

从Firebase加载数据,loadTableView():

func loadTableview(){

    let userID = Firebase.Auth.auth().currentUser?.uid

    databaseRef = Database.database().reference().child("DiabetesIOS").child(userID!)
    databaseRef.observe(.value, with: {(snapshot) in

        var newItems = [Post]()
        var newItemsDate = [String]()

        var aNewTime = [String]()
        var aNewBl = [String]()
        var aNewK = [String]()
        var aNewBo = [String]()
        var aNewBa = [String]()
        var aNewBlu = [String]()
        var aNewP = [String]()
        var aNewN = [String]()

        var dimArrayNewTime = [[String]]()
        var dimArrayNewBl = [[String]]()
        var dimArrayNewK = [[String]]()
        var dimArrayNewBo = [[String]]()
        var dimArrayNewBa = [[String]]()
        var dimArrayNewBlu = [[String]]()
        var dimArrayNewP = [[String]]()
        var dimArrayNewN = [[String]]()

        for year in snapshot.children.allObjects as! [DataSnapshot] {
            for month in year.children.allObjects as! [DataSnapshot] {
                for day in month.children.allObjects as! [DataSnapshot] {
                    for date in day.children.allObjects as! [DataSnapshot] {
                        for time in date.children.allObjects as! [DataSnapshot] {

                            let newPost = Post(snapshot: time)
                            newItems.append(newPost)

                            let newDate = Post(snapshot: time)
                            newItemsDate.append(newDate.snap6Date!)

                            if self.snapshotCheck == 0{
                                self.snapshotCheckDate = newDate.snap6Date
                                self.snapshotCheck += 1
                            }
                            else{
                            }

                            if self.snapshotCheckDate == newDate.snap6Date{
                                aNewTime.append(newDate.time)
                                aNewBl.append(newDate.blutzucker)
                                aNewKe.append(newDate.ke)
                                aNewBo.append(newDate.bolusInsulin)
                                aNewBa.append(newDate.basalInsulin)
                                aNewBlu.append(newDate.blutdruck)
                                aNewP.append(newDate.puls)
                                aNewN.append(newDate.notiz)

                            }
                            else{
                                dimArrayNewTime.append(aNewTime)
                                dimArrayNewBl.append(aNewBlutzucker)
                                dimArrayNewK.append(aNewKe)
                                dimArrayNewBo.append(aNewBolusinsulin)
                                dimArrayNewBa.append(aNewBasalinsulin)
                                dimArrayNewBlu.append(aNewBlutdruck)
                                dimArrayNewP.append(aNewPuls)
                                dimArrayNewN.append(aNewNotiz)

                                aNewTime.removeAll()
                                aNewBl.removeAll()
                                aNewK.removeAll()
                                aNewBo.removeAll()
                                aNewBa.removeAll()
                                aNewBlu.removeAll()
                                aNewP.removeAll()
                                aNewN.removeAll()

                                aNewTime.append(newDate.time)
                                aNewBl.append(newDate.blutzucker)
                                aNewK.append(newDate.ke)
                                aNewBo.append(newDate.bolusInsulin)
                                aNewBa.append(newDate.basalInsulin)
                                aNewBlu.append(newDate.blutdruck)
                                aNewP.append(newDate.puls)
                                aNewN.append(newDate.notiz)

                                self.snapshotCheckDate = newDate.snap6Date
                            }

                        }

                    }
                }
            }
        }
        self.postArrayValue = newItems

        self.aHeaderDate = newItemsDate
        self.aHeaderDate = self.uniq(source: self.aHeaderDate) 

        dimArrayNewTime.append(aNewTime)
        dimArrayNewBl.append(aNewBl)
        dimArrayNewK.append(aNewK)
        dimArrayNewBo.append(aNewBo)
        dimArrayNewBa.append(aNewBa)
        dimArrayNewBlu.append(aNewBlu)
        dimArrayNewP.append(aNewP)
        dimArrayNewN.append(aNewN)

        self.dimArrayTime = dimArrayNewTime
        self.dimArrayBl = dimArrayNewBl
        self.dimArrayK = dimArrayNewK
        self.dimArrayBo = dimArrayNewBo
        self.dimArrayBa = dimArrayNewBa
        self.dimArrayBlu = dimArrayNewBlu
        self.dimArrayP = dimArrayNewP
        self.dimArrayN = dimArrayNewN


        newItems.removeAll()
        newItemsDate.removeAll()

        aNewTime.removeAll()
        aNewBl.removeAll()
        aNewK.removeAll()
        aNewBo.removeAll()
        aNewBa.removeAll()
        aNewBlu.removeAll()
        aNewP.removeAll()
        aNewN.removeAll()

        dimArrayNewTime.removeAll()
        dimArrayNewBl.removeAll()
        dimArrayNewK.removeAll()
        dimArrayNewBo.removeAll()
        dimArrayNewBa.removeAll()
        dimArrayNewBlu.removeAll()
        dimArrayNewP.removeAll()
        dimArrayNewN.removeAll()

        self.tableView.reloadData()

    }){
        (error) in
        print(error.localizedDescription)
    }   
}

对于我的标题:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = Bundle.main.loadNibNamed("TableViewCell1", owner: self, options: nil)?.first as! TableViewCell1
    headerView.date.text = aHeaderDate[section]
    headerView.backgroundColor = UIColor.white
    return headerView
}

NumberOfRows:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dimArrayTime[section].count

}

标题数量:

override func numberOfSections(in tableView: UITableView) -> Int {
    return aHeaderDate.count
}

细胞数量:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dimArrayTime[section].count

}

部分:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell2 = Bundle.main.loadNibNamed("TableViewCell2", owner: self, options: nil)?.last as! TableViewCell2

    let sectionTime = dimArrayTime[indexPath.section][indexPath.row]
    let sectionBl = dimArrayBl[indexPath.section][indexPath.row]
    let sectionK = dimArrayK[indexPath.section][indexPath.row]
    let sectionBo = dimArrayBo[indexPath.section][indexPath.row]
    let sectionBa = dimArrayBa[indexPath.section][indexPath.row]
    let sectionBlu = dimArrayBlu[indexPath.section][indexPath.row]
    let sectionP = dimArrayP[indexPath.section][indexPath.row]
    let sectionN = dimArrayN[indexPath.section][indexPath.row]

    cell2.time.text = sectionTime
    cell2.bl.text = sectionBl
    cell2.k.text = sectionK
    cell2.bo.text = sectionBo
    cell2.ba.text = sectionBa
    cell2.blu.text = sectionBlu
    cell2.p.text = sectionP
    cell2.n.text = sectionN

    return cell2

}

addViewController:

@IBAction func saveInDatabaseFirebase(_ sender: Any) {

    let userID = Firebase.Auth.auth().currentUser?.uid

    let postRef = dataRef.child("DIOS").child(userID!).child(stringForDbYear).child(stringForDbMonth).child(stringForDbDay).child(stringForDbDate).child(stringForDbTime)

    sDate = tFDate.text!

    if tFBl.text == "" {
        sBl = "-"
    }
    else{
        sBl = tFBl.text!
    }
    if tFK.text == "" {
        sK = "-"
    }
    else{
        sK = tFK.text!
    }
    if tFBo.text == "" {
        sBo = "-"
    }
    else{
        sBo = tFBo.text!
    }
    if tFBa.text == "" {
        sBa = "-"
    }
    else{
        sBa = tFBa.text!
    }
    if tfBlu.text == "" {
        sBlu = "-"
    }
    else{
        sBlu = tfBlu.text!
    }
    if tFP.text == "" {
        sP = "-"
    }
    else{
        sP = tFP.text!
    }
    if tFN.text == "" {
        sN = "-"
    }
    else{
        sN = tFN.text!
    }


    let post = Post (date: stringForDbDate, time: stringForDbTime, day: stringForDbDay, bl: sBl, k: sK, bo: sBo, ba: sBa, blu: sBlu, p: sP, n: sN, snap1DIOS: "DIOS", snap2UserID: userID!, snap3Year: stringForDbYear, snap4Month: stringForDbMonth, snap5Day: stringForDbDay, snap6Date: stringForDbDate, snap7Time: stringForDbTime)
    postRef.setValue(post.toAnyObject())

    self.dismiss(animated: true, completion: nil); 
}

非常感谢您的帮助

0 个答案:

没有答案