3ph / CollectionPickerView

A generic customizable picker view based on UICollectionView.
MIT License
31 stars 10 forks source link

Multiple CollectionPickerViews: Thread 1: signal SIGABRT #5

Closed SubZane closed 5 years ago

SubZane commented 5 years ago

I have a custom cell called PillCell. When trying to have two CollectionPickerViews at the same time I get an SIGABRT Error.

        bodyPartPicker.dataSource = self
        bodyPartPicker.delegate = self
        bodyPartPicker.collectionView.register(PillCell.self, forCellWithReuseIdentifier: "body")

        typePicker.dataSource = self
        typePicker.delegate = self
        typePicker.collectionView.register(PillCell.self, forCellWithReuseIdentifier: "type")

however, using a standard collectionview works perfectly

        coll1.dataSource = self
        coll1.delegate = self
        coll1.register(PillCell.self, forCellWithReuseIdentifier: "one")

        coll2.dataSource = self
        coll2.delegate = self
        coll2.register(PillCell.self, forCellWithReuseIdentifier: "two")

So my guess is there's a bug here somewhere

3ph commented 5 years ago

It only crashes for me if I don't use the correct reuse identifier in the example project (in cellForItemAt function). Can you confirm you use the correct one in dequeReusableCell?

SubZane commented 5 years ago

I think I am, but here's my code just in case.

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.bodyPartPicker {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "body", for: indexPath) as! PillCell
            cell.label.text = bodyparts[indexPath.row].bodypart
            return cell
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "type", for: indexPath) as! PillCell
            cell.label.text = titles[indexPath.row]
            return cell
        }
    }

The code crashes on this row:

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "type", for: indexPath) as! PillCell
3ph commented 5 years ago

Ah I see. The problem quite likely is on this line: if collectionView == self.bodyPartPicker { The bodyPartPicker is CollectionPickerView so this check will always fail. Try to change it to if collectionView == bodyPartPicker.collectionView

SubZane commented 5 years ago

I think I found the error that made it crash

I need to reference the picker as well as the collectionView in my code, not necessary when using standard uicollectionviews

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.bodyPartPicker {
            let bodycell = bodyPartPicker.collectionView.dequeueReusableCell(withReuseIdentifier: "body", for: indexPath) as! PillCell
            bodycell.label.text = bodyparts[indexPath.row].bodypart
            return bodycell
        } else {
            let typecell = typePicker.collectionView.dequeueReusableCell(withReuseIdentifier: "type", for: indexPath) as! PillCell
            typecell.label.text = exerciseTypes[indexPath.row].type
            return typecell
        }
    }

However. I still get the same values on the cells for both pickers. As if the cell I use is the same cell, for both pickers instead two separate instances of the cell..

SubZane commented 5 years ago

I updated the file in your example by adding one more picker. Doesn't really work... I might be missing something but replacing it with a default uicollectionview will make it work.

Can you see if you can spot what's wrong? https://gist.github.com/SubZane/9421dcf48bcf607930df4b670b9bb7c5

3ph commented 5 years ago

Yes, as I said in my previous comment the picker is not UICollectionView so you can't compare those two directly. The code will always go the else branch trying to deque the "type" cell for both pickers - that's why it's crashing.

SubZane commented 5 years ago

so, any suggestion on how to change my IF/ELSE statement? :)

3ph commented 5 years ago

Try if collectionView == self.bodyPartPicker.collectionView {

SubZane commented 5 years ago

awesome! thanks :)

3ph commented 5 years ago

Cool, I'll close it then :).