4

I have a vertically scrolling UICollectionView that uses a subclass of UICollectionViewFlowLayout to try and eliminate inter-item spacing. This would result in something that looks similar to a UITableView, but I need the CollectionView for other purposes. There is a problem in my implementation of the FlowLayout subclass that causes cells to disappear when scrolling fast. Here is the code for my FlowLayout subclass: EDIT: See Comments For Update

class ListLayout: UICollectionViewFlowLayout {
    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
        if var answer = super.layoutAttributesForElementsInRect(rect) {
            for attr in (answer as [UICollectionViewLayoutAttributes]) {
                let ip = attr.indexPath
                attr.frame = self.layoutAttributesForItemAtIndexPath(ip).frame
            }
            return answer;
        }
        return nil
    }

    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
        let currentItemAtts = super.layoutAttributesForItemAtIndexPath(indexPath) as UICollectionViewLayoutAttributes
        if indexPath.item == 0 {
            var frame = currentItemAtts.frame
            frame.origin.y = 0
            currentItemAtts.frame = frame
            return currentItemAtts
        }
        let prevIP = NSIndexPath(forItem: indexPath.item - 1, inSection: indexPath.section)
        let prevFrame = self.layoutAttributesForItemAtIndexPath(prevIP).frame
        let prevFrameTopPoint = prevFrame.origin.y + prevFrame.size.height
        var frame = currentItemAtts.frame
        frame.origin.y = prevFrameTopPoint
        currentItemAtts.frame = frame
        return currentItemAtts
    }
}

One other thing to note: My cells are variable height. Their height is set by overriding preferredLayoutAttributesFittingAttributes in the subclass of the custom cell:

override func preferredLayoutAttributesFittingAttributes(layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes! {
    let attr: UICollectionViewLayoutAttributes = layoutAttributes.copy() as UICollectionViewLayoutAttributes
    attr.frame.size = CGSizeMake(self.frame.size.width, myHeight)
    return attr
}

And I set the layout's estimated size on initialization:

flowLayout.estimatedItemSize = CGSize(width: UIScreen.mainScreen().bounds.size.width, height: 60)

Here is a GIF that demonstrates this problem:

Problem

Does anybody have an idea as to what's going on? Your help is much appreciated.

Thanks!

Kyle Rosenbluth
  • 1,582
  • 4
  • 21
  • 38
  • Setting `minimumLineSpacing = 0` on the default UICollectionViewFlowLayout implementation was not sufficient? – i_am_jorf Oct 23 '14 at 20:47
  • Wow jeff, I thought I had tried that to no results, but it does seem to work now. That gets rid of my need for a subclass, but the collectionView still gets that black bar creeping up from the bottom when I scroll really fast. Any idea why? – Kyle Rosenbluth Oct 23 '14 at 20:55
  • I'm out of ideas. :-/ – i_am_jorf Oct 23 '14 at 22:07
  • I am facing exact same problem in my app now. How did you solve this issue? – Srikanth Jul 27 '15 at 17:28
  • I have the same problem and we are lucky enough to make it run on the iPhone cause when you run it to the iPad it will raise an exception. – John Paul Manoza Sep 10 '15 at 03:06

0 Answers0