简体   繁体   中英

How to track collectionView (backed by FetchedResultsController) cell

I have a chat collectionView. where user can send new message, delete messages etc. The collectionView is backed by a fetchedResultController.

There are multiple types of cells, such as, text, image, audio etc. I have created a player which handles audio for a single cell. The cell is the delegate of the player, so that I can update the playing time, stop the player etc from the Player object. I track the current audio playing cell using indexPath.

But the problem is, if the user deletes a message which is received or sent before the audio message, the indexPath of audio message changes.

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>,
                    didChange anObject: Any,
                    at indexPath: IndexPath?,
                    for type: NSFetchedResultsChangeType,
                    newIndexPath: IndexPath?) {
}

In this call back, I only receive delete for the deleted cell. But I don't receive any callback for the updated indexPath of the other cells. How can I track the audio cell indexPath or the cell if the previous cell is deleted?

I would suspect that it might be good if the struct or class of the data that you fill into each cell has a unique identifier, such that you can act on each object regardless of it's position on the screen. If your indexes are going to move around and change you might need something more permanent to keep track of which objects are to be acted upon.

Hopefully this makes sense. If you 'need' to act on the index of each cell instead of the data within, you will need to update any reference to the index whenever anything moves, deletes, or changes.

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