Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView reuse cell issue

I have a problem with collection view cell. When my collection view first loaded, it display items like that:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{


    CalendarCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"CalendarCell" forIndexPath:indexPath];

    [cell bindDate:_datesMgr.currentMonthItems[indexPath.row] andNowDate:_datesMgr.nowDate];


    // bind events

    if (_eventsMgr.eventsArray.count > 0){

        for (int i = 0; i < _eventsMgr.eventsArray.count ; i ++) {
            [cell bindConference:_eventsMgr.eventsArray[i]];
        }
    }


    return cell;
}

Inside those methods are logic for adding subviews to custom cell class, which depend on certain circumstances.

Its all work, but, when collection view reloaded (i did force reload after 1 second) some of cells are reused and placed on others, therefore, it show "old" images and subviews.

I could see possible solution in forcing uicollection view to stop reusing cells (it is, load new cells every time). Is there any way to do this?

like image 565
Evgeniy Kleban Avatar asked Jul 19 '16 10:07

Evgeniy Kleban


People also ask

How do I reuse a Collectionview cell?

If you're working entirely in code, you can register a UICollectionViewCell subclass for use with your collection view, so that new cells are dequeued and re-use automatically by the system. If a cell doesn't already exist that can be re-used, a new one will be created automatically.

What is the difference between UITableView and UICollectionView?

For the listing details of each item, people use UITableView because it shows more info on each item. The UICollectionView class manages an ordered collection of data items and presents them using customizable layouts.

How does UICollectionView work?

UICollectionView makes adding custom layouts and layout transitions, like those in Photos, simple to build. You're not limited to stacks and grids because collection views are customizable. You can use them to make circle layouts, cover-flow style layouts, Pulse news style layouts and almost anything you can dream up!

How do I remove cells from Collectionview?

Run ProjectBuild and Run the project and select the Edit Button. Select a few cells and press the Trash button to remove the items.


1 Answers

Try to implement prepareForReuse method for reset your old content in your custom UICollectionViewCell

-(void)prepareForReuse {
   [super prepareForReuse];
   self.yourimageview.image = nil; //and etc.
}
like image 83
iSashok Avatar answered Oct 10 '22 19:10

iSashok