我正在使用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
}
}
答案 0 :(得分:0)
您只能仅调用reloadData
,beginUpdates
和所有其他与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) }