2017-01-03 4 views
0

채팅 메시지를 시뮬레이션하려고하는데 몇 가지 새로운 셀을 삽입 한 후 가장 오래된 것 중 일부가 사라집니다. 그리고 스크롤을 다시하고 사라지면. 나는 여기에서 찾은 모든 해결책을 시도했지만 아무 것도 작동하지 않으며 오류가 올 수있는 곳이별로 없다.빈 셀이 나타나며 무작위로 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 
    } 
} 
+0

어떻게'getMessageCell'을 사용하고 있습니까? 어디에서 호출할까요? –

+0

배열에 셀을 저장하고 배열에 메시지 객체 만 저장하고 셀 코드를 셀의 셀 코드에 추가한다고 생각하지 마십시오. 다른 모양으로 보일 경우 여러 개의 사용자 정의 셀을 사용하십시오 – Tj3n

+0

Jon Rose는 왜 내가 셀을 가진 배열을 사용했는지 아래에 있지만, 그것을 다시 넣으려고합니다. – Eironeia

답변

0
  1. 없애을 dispatch_async 이미 주요 에 있어야합니다 실.
  2. Model 객체의 배열을 셀 배열이 아닌 배열로 유지하십시오. displayedMessage의 배열이어야하는 것처럼 보이면 케이스에 나타납니다.
  3. 또한 이러한 셀을 에 재사용 할 수 있으므로 사용자가 설정 한 모든 속성을 항상 업데이트해야합니다. 다시 말해 if은 셀을 구성 할 때 else이어야합니다.

희망이 있습니다.

+0

안녕하세요, 답변 주셔서 감사합니다, 나는 그것을 (dispatch_async) 제거하지만 스크롤 문제가 너무 많은 시간이 걸립니다 때문에 테이블 뷰에서 200 개 이상의 메시지가 너무 많은 시간이 걸립니다. 그래서 이것은 많은 셀이있는 스크롤 할 때마다 getMessageCell을 수행하는 것을 피하기 위해 셀 배열을 시도한 이유입니다. 나는 if와 if의 내부에서 리턴을 사용하므로, 필자는 els 문을 사용하는 것과 같습니다. 하지만 문제가 발생했습니다. 추가 할 때 나머지 요소는 "미친 듯"나타나서 사라졌습니다. – Eironeia

+0

UITableview에는 재사용되는 tableviewCell 풀이 있습니다. 프레임을 떠날 때 다른 셀이 프레임에 들어올 때 다시 사용할 수 있도록 풀에 넣습니다. 이것은 매우 빠르고 효율적입니다. 200 개의 메시지는 다루기에 tableview가 많지 않습니다. 만약 당신의'getMessageCell' 메쏘드가 단지 몇개의 라벨과 이미지를 설정하는 것보다 더 많은 일을한다면, 당신은 뭔가 잘못하고있는 것입니다. –

+0

셀의 배열을 피하는 것 같지만 시간이 너무 많이 걸리는 스크롤 문제가 있습니다. http://stackoverflow.com/questions/41422841/scroll-to-last-element-of-tableview-take- 너무 많은 시간 – Eironeia