Chris
Chris

Reputation: 2747

NSCollectionView (osx 10.11) hangs when running

I've made a subclass of NSCollectionView which conforms to NSCollectionViewDataSource, NSCollectionViewDelegate but it hangs everytime - I only get the spinning beach ball.

public class SequenceCollectionView : NSCollectionView, NSCollectionViewDataSource, NSCollectionViewDelegate {

    // MARK: Inits

    public required init(coder aDecoder: NSCoder)  {
        super.init(coder: aDecoder)!
        commonInit()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    func commonInit() {
         self.dataSource = self
        self.delegate = self
    }


    //MARK: Datasource

    public func collectionView(collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }


    public func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem {

        if indexPath.item == 0 {
            let item = NSCollectionViewItem(nibName: "ItemView", bundle: nil)
        }

        return NSCollectionViewItem()
    }
}

It's obviously in some kind of loop or retain cycle but I'm finding it hard to debug.

enter image description here

Upvotes: 3

Views: 328

Answers (2)

Charlton Provatas
Charlton Provatas

Reputation: 2274

Adding to Chris' answer, the NSCollectionView will hang on scrolling or resizing the view unless the following optional delegate method is overriden:

- (void)collectionView:(NSCollectionView *)collectionView didEndDisplayingItem:(NSCollectionViewItem *)item forRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {}

Upvotes: 2

Chris
Chris

Reputation: 2747

It transpires that the issue lies with the optional delegate call

public func collectionView(collectionView: NSCollectionView, willDisplayItem item: NSCollectionViewItem, forRepresentedObjectAtIndexPath indexPath: NSIndexPath) {
}

By adding this to the class has fixed the issue.

Upvotes: 4

Related Questions