简体   繁体   English

快速重新加载表视图后,表视图未更新

[英]Table view is not updating after reload table view in swift

I am using socket.io for instant chatting.我正在使用 socket.io 进行即时聊天。

I went message list screen from User List ,first time its working fine.我从用户列表转到消息列表屏幕,第一次工作正常。

Message has been sent and receive properly first time but i came back again on message list, Some glitch are occurring and on sending and receiving any message tableview is not updating while table view is reloading.消息已第一次正确发送和接收,但我又回到了消息列表中,发生了一些故障,并且在重新加载表视图时发送和接收任何消息表视图没有更新。

on sending or receiving message given module are calling发送或接收消息给定模块正在调用

In ViewDidAppear在 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
        }
    }

You have to call reloadData , beginUpdates and all other UI-related tasks in main queue only .需要在主队列中调用reloadDatabeginUpdates和所有其他 UI 相关的任务。

wrap all your UI-related lines in this:将所有与 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) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速调用dismissViewController后如何调用重载表视图? - How to call reload table view after you call dismissViewController in swift? 解除模态视图后重新加载表视图 - reload table view after modal view dismissal 重新加载Table View无法从Backendless正确更新 - Table View reload Not updating correctly from Backendless 在swift 3中单击另一个表视图单元格的集合视图数据后,如何在表视图单元格中重新加载集合视图数据? - How to reload the collection view data in table view cell after clicking another table view cell's collection view data in swift 3? 使用Swift在表视图中重新加载单元格数据 - reload cell data in table view with Swift 使用动画重新加载表格视图单元格(Swift) - Reload Table view cell with animation (Swift) 添加联系人时重新加载表视图 swift - reload table view when Contact Added swift 发布后更新表视图 - Updating Table View After Publish 从 firebase 获取数据后如何刷新容器视图(重新加载表视图):Swift - How to refresh container view (reload table view) after getting data from firebase: Swift 如何通过快速点击集合视图的单元格在表视图中重新加载数据 - How to reload data in table view by tapping the cell of collection view in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM