简体   繁体   中英

Cell reuse bug in collectionView

Hello to all dear friends. I'm experiencing a problem with cell reuse. In practice I select multiple cells. In the "didSelected" method, I modify a property, called "isSelectedCell", to true and add a green border (to indicate the selection); While in the "didDeselect" method I carry it to false and remove the color of the border. But when I scroll down some cells, never selected, appear to be selected and the property is true. Because? How do I prevent this. It seems that when a cell is reused, the properties take on old ones and not their own.

If you are using a custom cell, override prepareForReuse and reset all properties to default values

override func prepareForReuse() {
    super.prepareForReuse()
    // reset custom properties to default values
}

Cells are for reuse means they are only fixed number of views which are used again. You have to update cells for every item to be displayed in that cell in collectionView cellForItemAtIndexPath method. You've to put it inside data source like array of objects to return correct value for every item. Hence it will remember to display which property on item. Reusable cells are necesary to not bloat up the device memory and reuse same views.

it's due to the cell reuse of CollectionView.

as you can see in this article

when a cell disappear, it's reused to be the new one that going to appear.

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