【发布时间】: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