Messagekit: Why is cellForItemAt dequeuing non existing cells?

Created on 13 Jul 2019  路  3Comments  路  Source: MessageKit/MessageKit

Greetings!
I'm implementing a feature where if a message doesn't get delivered a red notification appears on the right side of the message. To do this I have a SendError() function and also a Boolean variable in each data array instance that tells the cell if it should display an error. The problem is that for some reason sometimes when dequeued the generated cell is displayed incorrectly.

Normal cell:
Screen Shot 2019-07-13 at 3 52 31 PM

Also a normal cell:
Screen Shot 2019-07-13 at 3 52 26 PM

Super weird paranormal cell:
Screen Shot 2019-07-13 at 3 55 00 PM

The paranormal cell is in the normal, non-error position and half of the notification is visible.
I need the MessageCollectionView to remember, and correctly display the error notifications until they are dealt with by the user - 2 cell types: error, no error. How do I accomplish this and what am I doing wrong?

This is my CellForRowAt:


override open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = super.collectionView(collectionView, cellForItemAt: indexPath)
    cell.contentView.subviews.forEach ({
        if $0 is UIButton {
            print("REMOVED")
            $0.removeFromSuperview()
        }
    })

    if messageList[indexPath.section].errorShouldShow {
        sendError(cell: cell, index: indexPath, animates: false)
        return cell
    }

    cell.contentView.frame = CGRect(x: cell.frame.minX - 8, y:  cell.contentView.frame.origin.y, width: cell.contentView.frame.width, height: cell.contentView.frame.height)
    return cell
}

This is my SendError function:

func sendError(cell: UICollectionViewCell, index: IndexPath, animates: Bool)  {

    let errorNotif: UIButton = UIButton(frame: CGRect(x: self.view.frame.maxX - 18, y: cell.contentView.frame.maxY - 24, width: 24, height: 24))
    errorNotif.setImage(#imageLiteral(resourceName: "error"), for: .normal)
    cell.contentView.addSubview(errorNotif)

    if animates {
        UICollectionViewCell.animate(withDuration: 0.1, animations: {

            cell.contentView.frame = CGRect(x: cell.frame.minX - 28 , y:  cell.contentView.frame.origin.y, width: cell.contentView.frame.width, height: cell.contentView.frame.height)



        }, completion: {(finished: Bool) in


        })
    } else {
        cell.contentView.frame = CGRect(x: cell.frame.minX - 28 , y:  cell.contentView.frame.origin.y, width: cell.contentView.frame.width, height: cell.contentView.frame.height)
    }
}

And this is my message send failure handler:


} else {
    let visibleIndexPaths = self.messagesCollectionView.indexPathsForVisibleItems

    self.messageList[index.section].errorShouldShow = true
    if let cell = self.messagesCollectionView.cellForItem(at: index) {
        if visibleIndexPaths.contains(index) {
            self.sendError(cell: cell, index: index, animates: false)
        }
    }
}

I'm very new to using MessageKit so surely I missed something. Any help would be greatly appreciated!

question stale

Most helpful comment

Without me taking a super detailed look since this is pretty client specific, I would suspect the error comes from you modifying the cells content view frame. I don鈥檛 think that is the best approach as it鈥檚 the collection views responsibility to do that.

I would suggest subclassing a cell rather than changing things dynamically, or, try utilizing the accessory view of the cell.

Sent with GitHawk

All 3 comments

Without me taking a super detailed look since this is pretty client specific, I would suspect the error comes from you modifying the cells content view frame. I don鈥檛 think that is the best approach as it鈥檚 the collection views responsibility to do that.

I would suggest subclassing a cell rather than changing things dynamically, or, try utilizing the accessory view of the cell.

Sent with GitHawk

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

This issue has been auto-closed because there hasn't been any activity for at least 21 days. However, we really appreciate your contribution, so thank you for that! 馃檹 Also, feel free to open a new issue if you still experience this problem 馃憤.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ChandraPrakashJangid picture ChandraPrakashJangid  路  3Comments

JulienLevallois picture JulienLevallois  路  4Comments

emmanuelay picture emmanuelay  路  3Comments

TheDarkCode picture TheDarkCode  路  3Comments

yspreen picture yspreen  路  3Comments