채팅 메시지를 시뮬레이션하려고하는데 몇 가지 새로운 셀을 삽입 한 후 가장 오래된 것 중 일부가 사라집니다. 그리고 스크롤을 다시하고 사라지면. 나는 여기에서 찾은 모든 해결책을 시도했지만 아무 것도 작동하지 않으며 오류가 올 수있는 곳이별로 없다.빈 셀이 나타나며 무작위로 UITableView에서 사라진다.
나는 당신에게 도움을 요청한 코드를 게시해야할지 모르겠다. TableView 코드를 게시하여 어쩌면 내가 뭔가 잘못하고 있거나 다른 것을 필요로 할 때 알려주겠다.
func getMessageCell(withDisplayedMessage displayedMessage: GroupChatMessages.GetChatMessages.displayedChatMessage) -> GroupChatCell {
switch displayedMessage.messageType {
case 0:
if displayedMessage.sender == self.currentUser.userID {
let cell = self.messagesTableView.dequeueReusableCellWithIdentifier("senderCell") as! GroupChatCell
dispatch_async(dispatch_get_main_queue()) {
cell.configureCellText(withText: displayedMessage.text, andUtcSendTime: displayedMessage.utcSendTime)
cell.selectionStyle = UITableViewCellSelectionStyle.None
}
return cell
}
let cell = self.messagesTableView.dequeueReusableCellWithIdentifier("receiverCell") as! GroupChatCell
cell.configureCellAttributted(withText: displayedMessage.text, andSenderName: displayedMessage.senderName, andUtcSendTime: displayedMessage.utcSendTime)
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
case 1:
let cell = self.messagesTableView.dequeueReusableCellWithIdentifier("announcementCell") as! GroupChatCell
dispatch_async(dispatch_get_main_queue()) {
cell.configureInformationCell(withText: displayedMessage.text)
cell.selectionStyle = UITableViewCellSelectionStyle.None
}
return cell
case 2:
if displayedMessage.sender == self.currentUser.userID {
let cell = self.messagesTableView.dequeueReusableCellWithIdentifier("senderImageCell") as! GroupChatCell
dispatch_async(dispatch_get_main_queue()) {
cell.configureSenderImageCell(withImageUrl: displayedMessage.text, andUtcSendTime: displayedMessage.utcSendTime)
cell.selectionStyle = UITableViewCellSelectionStyle.None
}
return cell
}
let cell = self.messagesTableView.dequeueReusableCellWithIdentifier("receiverImageCell") as! GroupChatCell
dispatch_async(dispatch_get_main_queue()) {
cell.configureImageCell(withImageUrl: displayedMessage.text, andSenderName: displayedMessage.senderName, andUtcSendTime: displayedMessage.utcSendTime)
cell.selectionStyle = UITableViewCellSelectionStyle.None
}
return cell
case 10: //SpecialCaseForSendingImages
let cell = self.messagesTableView.dequeueReusableCellWithIdentifier("senderImageCell") as! GroupChatCell
dispatch_async(dispatch_get_main_queue()) {
cell.configureSenderImageCell(withImageUrl: displayedMessage.text, andUtcSendTime: displayedMessage.utcSendTime)
cell.selectionStyle = UITableViewCellSelectionStyle.None
}
return cell
default:
return GroupChatCell()
}
당신이 도움이 될 수 있습니다 희망, 그리고 어떤 추가 정보는 내가 빨리 내가 할 수있는 당신을 제공 할 것입니다 : 나는 매번 새로운 메시지가 삽입 된 세포를 생성하는 방법을
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.messagesCell.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return self.messagesCell[indexPath.row]
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let message = self.messages[indexPath.row]
if message.messageType == 2 {
output.setImageUrl(message.text)
router.navigateToGroupChatMessagesScene()
}
else {
self.view.endEditing(true)
}
}
이 코드입니다! 정말 고맙습니다.
편집 : 나는이 기능에 메시지 정보에 새 행을 추가 새로운 메시지가 나타납니다
:
func displayMessages(viewModel: GroupChatMessages.GetChatMessages.ViewModel) {
let displayedMessage = viewModel.displayedMessages
print ("i'm here!")
if let messages = displayedMessage {
self.messages = messages
self.messagesCell = []
for index in 0..<messages.count {
let cell = self.getMessageCell(withDisplayedMessage: messages[index])
self.messagesCell.append(cell)
let indexPath = NSIndexPath(forRow: index, inSection: 0)
self.messagesTableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
print ("i'm here2!")
firstTime = false
// self.scrollToLastMessage(false)
self.setVisible(hiddenTableView: false, hiddenChatLoader: true)
self.messagesLoaded = true
}
}
어떻게'getMessageCell'을 사용하고 있습니까? 어디에서 호출할까요? –
배열에 셀을 저장하고 배열에 메시지 객체 만 저장하고 셀 코드를 셀의 셀 코드에 추가한다고 생각하지 마십시오. 다른 모양으로 보일 경우 여러 개의 사용자 정의 셀을 사용하십시오 – Tj3n
Jon Rose는 왜 내가 셀을 가진 배열을 사용했는지 아래에 있지만, 그것을 다시 넣으려고합니다. – Eironeia