【问题标题】:Table view is not updating after reload table view in swift快速重新加载表视图后表视图未更新
【发布时间】:2020-03-19 11:40:35
【问题描述】:

我正在使用 socket.io 进行即时聊天。

我从用户列表进入消息列表屏幕,第一次工作正常。

消息已在第一次正确发送和接收,但我再次返回消息列表,发生了一些故障,并且在发送和接收任何消息时,表格视图在重新加载表格视图时没有更新。

在发送或接收给定模块正在调用的消息时

在 ViewDidAppear 中

    **// on sending or receiving message given module are calling**

    SocketIOManager.sharedInstance.getChatMessage { (messageInfo) -> Void in

        //----------- Get value from Resulted array --------------
        self.txtField_Chat.text = ""
        self.isSendBtnEnabled(val: false)

        let result_arr = messageInfo["data"] as! NSArray
        let result_dic = result_arr[0] as? NSDictionary

        let result_string_date : String = result_dic!.value(forKey: "date") as! String
        let result_array : NSArray = result_dic!.value(forKey: "value") as! NSArray
        let result_dic_value : NSDictionary = result_array[0] as! NSDictionary
        // ----------------------------------------------
        // ----------- Get value from current array --------------
        if self.arr_message_All.count > 0{
            let recentdate_dic : NSDictionary = self.arr_message_All[self.arr_message_All.count - 1] as NSDictionary
            let recentdate : String = recentdate_dic.value(forKey: "date") as! String
            let recentdate_array : NSArray = recentdate_dic.value(forKey: "value") as! NSArray
            let recentdate_arrayM : NSMutableArray = recentdate_array.mutableCopy() as! NSMutableArray
            let dic_message = Message(dictionary: result_dic_value.value(forKey: "message") as! NSDictionary)
            // ----------------------------------------------
            var get_msg_id : String = ""
            if let msg_id = defaults.value(forKey: "m_id") as? String{
                get_msg_id = msg_id
            }
            if !(dic_message?.msg_m_id == get_msg_id){

                if (messageInfo["status"] as! NSString) == "Success"{
                    print("********************",dic_message!.msg_value)
                    //
                    if recentdate == result_string_date{
                        //                                DispatchQueue.main.async {
                        print("Before insertion row count ",recentdate_arrayM.count)
                        recentdate_arrayM.add(result_dic_value)
                        print("Afer insertion row count ",recentdate_arrayM.count)
                        var dic = NSDictionary()
                        dic = ["date":recentdate , "value" : recentdate_arrayM]
                        self.arr_message_All[(self.arr_message_All.count) - 1] = dic
                        // First figure out how many sections there are
                        let lastSectionIndex = self.tblView_chat.numberOfSections - 1
                        // Then grab the number of rows in the last section
                        let lastRowIndex = self.tblView_chat!.numberOfRows(inSection: lastSectionIndex)
                        // Now just construct the index path
                        let pathToLastRow = NSIndexPath(row: lastRowIndex, section: lastSectionIndex)
                        print("in Socket",lastSectionIndex,lastRowIndex)
                        self.tblView_chat.reloadData()
                        self.tblView_chat.beginUpdates()
                        //                                    self.tblView_chat.insertRows(at: [(pathToLastRow as IndexPath)], with: .automatic)
                        self.tblView_chat.endUpdates()
                        self.tblView_chat?.scrollToRow(at: pathToLastRow as IndexPath as IndexPath, at: UITableView.ScrollPosition.none, animated: true)
                    }
                    else{
                        self.arr_message_All.append(result_dic!)
                        self.tblView_chat.reloadData()
                        self.scrollToBottomOfChat()
                        //                            })
                    }
                }
                //end Of If Condition
            }
            defaults.set(dic_message?.msg_m_id, forKey: "m_id") //setObject
        }
    }

【问题讨论】:

    标签: ios swift uitableview socket.io


    【解决方案1】:

    您必须在主队列中调用reloadDatabeginUpdates 和所有其他与 UI 相关的任务

    将所有与 UI 相关的行包含在以下内容中:

    DispatchQueue.main.async {
    
         // all UI-related stuff
         self.tblView_chat.reloadData()
         self.tblView_chat.beginUpdates()
         self.tblView_chat.insertRows(at: [(pathToLastRow as IndexPath)], with: .automatic)
         self.tblView_chat.endUpdates()
    
         self.tblView_chat?.scrollToRow(at: pathToLastRow as IndexPath as IndexPath, at: UITableView.ScrollPosition.none, animated: true)
    
    }
    

    【讨论】:

    • 为了调试,我打印了 indexpath 行和节的值,第一次打印错误的值。
    • 我在这里的另一条评论是,致电.numberOfSections.numberOfRows(inSection 获取信息是不正确的。这是一种反模式。您正在从不是该数据所有者的视图中获取数据。您必须在变量中保存所需的信息并从表格视图中访问它们。不是相反。
    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2019-06-10
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 2014-11-13
    相关资源
    最近更新 更多