简体   繁体   中英

Blank cells appear and dissapear randomly in UITableView

I'm trying to simulate a Chat Messages, and after insert some new cells, some of the oldest dissapear. And when I scroll appears again and disappear. I've tried all solutions that I found from here on SO but nothing works and I have not much idea frorm where error can come.

I'm not sure what code should I post to you tried to help, I will post my TableView code so maybe I'm doing something wrong or if you need anything else, just let me know.

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)
    }

}

This code is how I generate the cells everytime a new message is inserted:

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()
    }

Hope you can help, and any further information I will provide you as fast I can! Thank you so much.

EDIT:

Where I receive a new message I add a new row with message information in this function:

    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
    }
}
  1. Get rid of the dispatch_async you should already be on the main thread.
  2. Keep an array of Model objects NOT an an array of cells (in your case it looks like it should be an array of displayedMessage ).
  3. Also remember that these cells can be reused, so any property that you set must always be updated. In other words every if must have an else when configuring a cell.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM