Cell of UICollectionView not always refreshed after a scroll

12,549

Solution 1

This seems to be a bug in UICollectionView when using sectionInsets. The cells don't get invisible, they are misplaced out of bounds sometimes on fast scrolling.

So if you normally would have 3 items per row, you will get a forth item in one row, but no item at the first spot of the next row.

If you set your UICollectionView to not clip to bounds, and move it on top of your UI, you might see this misaligned cell.

My solution was so far to not use sectionInsets, and surround it by using contentInset and some empty space in the section headers.

I also found some other guys at Twitter with the same problem. There's also a bug report about it: 12353513

Solution 2

I've had a similar issue. I fixed it by forcing a repaint when setFrame: is called on my custom content. In your case, subclass CPTGraphHostingView and override this method:

-(void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    [self setNeedsDisplay]; // force drawRect:
}

Edit: An easier way to refresh cell views when they are reused is to override applyLayoutAttributes: in iPadCellCollectionViewCell as follows:

-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    // apply custom attributes...
    [self.hostingView setNeedsDisplay]; // force drawRect:
}

Regarding your workaround: there will be more and more subviews in the content view, which probably isn't a good thing. However, if you'd remove any old subview before adding the new one, that should be OK, too.

Share:
12,549

Related videos on Youtube

sebastien
Author by

sebastien

Updated on June 04, 2022

Comments

  • sebastien
    sebastien almost 2 years

    I have defined in my Storyboard (iPad) a view that contains a UICollectionView, at the bottom of the view there's also a UIToolbar. In the UICollectionView I have added a UICollectionViewCell (implemented by an iPadCellCollectionViewCell class) that contains a another view that is a Core-Plot Graph (a CPTGraphHostingView class).

    I have a class called X that implements the UICollectionViewDelegate and UICollectionViewDataSource.

    In the class X, I build for each cell of my view (in ViewDidLoad) the Core-Plot Graph and I have the following code to link the graph to the cell of the UICollectionViewCell

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* cellId = @"ChartCollectionId"; 
    
    iPadCellCollectionViewCell* cell = [self.theCollectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
    MyChart* chart = [_charts objectAtIndex:indexPath.row];
    
    cell.hostingView.hostedGraph = chart.barChart;
    
    return cell;
    

    }

    It's working fine, all my Graphs are correctly displayed on the screen. The issue happens when the user scroll the view, then one or several cells become "blank". I really don't understand why.

    I have found a workaround, instead of adding the CPTGraphHostingView in the UICollectionViewCell in the Interface Builder, I build it myself and the previous code becomes:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* cellId = @"ChartCollectionId"; 
    
    iPadCellCollectionViewCell* cell = [self.theCollectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
    
    [cell.contentView addSubview:[_hostingViews objectAtIndex:indexPath.row]];
    
    return cell;
    

    }

    With this code everything is working fine. Is there any explanations why it's not working when I'm adding the CPTGraphHostingView in the UICollectionViewCell with the IB?

    Is it an issue to call "addSubview" each time the method is called? No memory leaks?

  • Adam McKee
    Adam McKee about 11 years
    I have the same problem with cells rendering in the wrong location when scrolling horizontally, but unfortunately, neither of these two fixes worked for me.
  • bandejapaisa
    bandejapaisa almost 11 years
    Where do i view that bug report?