Mike Strong
Mike Strong

Reputation: 950

why are my collectionViewCell's displaying unusual behavior?

For some reason, my collectionViewCell's are unrecognized when they are selected. It's not until another cell is touched afterwards that the previous cell is recognized. To explain how I realized this, I added the following code to my collectionView's didDeselectItemAtIndexPath method: println("user tapped on cell # \(indexPath.row)"). When I run the app and select a cell, my console doesn't respond until I tap another cell, then it reads the println I added. For instance, if I select the first cell, the debugger doesn't print anything until i select another cell, then the console reads "user tapped on thumbnail # 0".

Now I've added an animation to my collectionView that enlarges each cell on selection, so this is how I know it isn't an indexPath issue because the cell indexPath # that is printed in the console is the correct cell that is enlarged in the view, but like i said, the cell isn't animated on its selection, not until i select another cell afterwards.

Here is my collectionView delegate and dataSource logic:

 // MARK: UICollectionViewDataSource
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return self.books.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCollectionViewCell

        // Configure the cell
        let book = self.books[indexPath.row]
        let coverImage = book.coverImage
        if coverImage == nil {
            book.fetchCoverImage({ (image, error) -> Void in
                if self.collectionView != nil {

                    collectionView.reloadItemsAtIndexPaths([indexPath])
                }
            })
        } else {
            let imageView = cell.imageView
            imageView.image = book.coverImage
        }

        return cell
    }

    override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell

        let book = self.books[indexPath.row]

        self.selectedImageView = cell.imageView

        if !isModeModal {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewControllerWithIdentifier("DetailViewController") as! DetailViewController

            controller.imageSelected = book.coverImage

            self.navigationController?.pushViewController(controller, animated: true)

        }

        println("user tapped on thumbnail # \(indexPath.row)")

    }
}

Why is this behavior occurring?

Upvotes: 0

Views: 55

Answers (1)

Skyfox
Skyfox

Reputation: 85

I didn't run your code, but I think, that the problem could be caused by didDeselectItemAtIndexPath. Try to use didSelectItemAtIndexPath instead.

If necessary you can add deselectItemAtIndexPath :

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    collectionView.deselectItemAtIndexPath(indexPath: NSIndexPath, animated: Bool)

}

Upvotes: 1

Related Questions