GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

@IBDesignable CollectionView leads to Xcode crash

Forums > iOS

Hi!

I have an @IBDesignable UICollectionView

@IBDesignable
class CollectionView: UICollectionView {

    private let cellId = "CollectionViewCell"

    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setup()
    }

    private func setup() {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: cellId, bundle: bundle)
        register(nib, forCellWithReuseIdentifier: cellId)

        dataSource = self
        delegate = self
    }
}

extension CollectionView: UICollectionViewDataSource {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
    }
}

I've noticed that when the Collection View number of items in the attribute inspector is less than func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) returns, it leads to Xcode crash on storyboard rendering.

Xcode

For example, this

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

would lead to the crash. The application in the same time works fine in a simulator.

I think this is somehow related to dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath). Is there a way to cope the problem?

3      

Update: removing the line

        delegate = self

helps to prevent the Xcode crash and prints Crashed status inside the attribute inspector. Crashed

3      

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.