Igor Kuznetsov
Igor Kuznetsov

Reputation: 108

Hide view in specific CollectionViewCell

I have question how to implement this stuff in right way. So far I done

if indexPath.row == 1 {
            let indexPatha = NSIndexPath(forRow: 0, inSection: 0)

            let changeCell = collectionView .cellForItemAtIndexPath(indexPatha) as! BarCollectionViewCell
            changeCell.addNewBottleSecondButton.alpha = 0
}

But when I swipe until cell is hidden, I am getting error, unexpectedly found nil while unwrapping an Optional value, and still this doesn't looks like how I want to make it. I want to achieve that when I have more then one cell, I want to hide one specific view.

Upvotes: 1

Views: 53

Answers (2)

Jed Bontjes
Jed Bontjes

Reputation: 36

If the cell has been scrolled off the screen it may not exist anymore due to Apple's dequeue/re-use optimizations. Previously when confronted with this problem, I've had to set a state variable and handle the UI change in cellForRow if the cell didn't exist when trying to change the cell's UI.

Upvotes: 0

GetSwifty
GetSwifty

Reputation: 7746

Would it work in your flow to handle this in cellForRowAtIndexPath instead?

After your initialize your cell:

    cell.addNewBottleSecondButton.hidden = (indexPath.row == 0) && (dataItems.count > 1)

Upvotes: 1

Related Questions