簡體   English   中英

無法正確重新加載TableView Swift iOS

[英]Unable to reload tableview properly swift ios

我有一個tableview,它是返回問題列表的導航控制器的一部分。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath) as! AnswerTableViewCell
    let data = quesList[indexPath.row]

    cell.labelName.text = data.sender_name;
    cell.labelQuestion.text = data.text;

    return cell
}

func fetchQues(){
    Ref.queryOrdered(byChild: currentuser).queryEqual(toValue: nil).observe(DataEventType.value, with:{(snapshot)in
        if snapshot.childrenCount>0{
            self.quesList.removeAll()
            for data in snapshot.children.allObjects as![DataSnapshot]{
                let dataObject = data.value as? [String: AnyObject]
                let userid = dataObject?["sender_id"];
                let username = dataObject?["sender_name"];
                let questext = dataObject?["text"];
                let questionid = dataObject?["id"];

                let data = Question(id: questionid as! String, sender_id: userid as! String, sender_name: username as! String, text: questext as! String)

                self.quesList.insert(data, at :0)

            }
            self.AnswerTable.reloadData()
        }
    })
}

然后,我有一個didselectrow函數,用於選擇此導航堆棧的VC部分,該部分允許用戶輸入問題的答案。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let data = quesList[indexPath.row]

    questionselect1 = data.text;
    questionid1 = data.id;

    performSegue(withIdentifier: self.questSegue, sender: nil)
}

用戶提交問題答案后,它將字典添加到問題字典中,並返回到問題屏幕列表。 queryorder(bychild:currentuser).queryeuqual(tovalue:nil)將僅返回當前用戶尚未回答的問題。 因此,每當用戶回答問題時,該問題就會從列表中刪除。

  @IBAction func submitAnswer(_ sender: Any) {
    if questiontext.text != "" {
        DBProvider.Instance.postAnswers(senderID: userid, senderName: username2, text: questiontext.text!)
        DBProvider.Instance.postAnswered()
        self.questiontext.text = "";
        self.navigationController!.popViewController(animated: true)
        //append data to the current question


    } else {
        alertTheUser(title: "Please Submit A Valid Answer", message: "Question Field Cannot Be Empty");

    }

}

目前,這很好用,但是,當列表中只有一個問題,並且用戶回答了該問題,並返回到問題表視圖時,最后一個問題不會從列表中刪除,因為它有被回答。

我只需要補充一點,當我完全退出我的應用程序並再次登錄時,問題清單為空,並且刷新正確,因為所有問題均已得到回答。

我一直在嘗試通過更改segueing方法並用viewwillload或viewdidload重新加載視圖來弄清楚為什么會發生這種情況,但它不起作用。

希望有人能理解我的意思,並可以為我的錯誤提供答案。

如果不查看代碼,很難確切說出,但這可能是由於其中一個View Controller的根UIView上沒有設置背景色引起的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM