0

I have collectionView with images in each cell. I want to see the full image when tapping on a cell.

I know this can be done with the selectItemAtIndexPath method and that I need to do something with the selected Cell:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
   println("Row: \(indexPath.row) is selected")

   var cell:UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
}

I've seen a lot of code examples in objc, like here, but I can't figure it out in Swift.

Thank you in advance


Update:

My code so far:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{   
    collectionView.collectionViewLayout.invalidateLayout()
    let cell = collectionView.cellForItemAtIndexPath(indexPath)
    let animateChangeWidth = { [weak cell] in
        let frame:CGRect = cell.frame
        frame.size = cell.intrinsicContentSize()
        cell.frame = frame
    }
    UIView.transitionWithView(cell, duration: 0.7, options: UIViewAnimationOptions.CurveLinear, animations: animateChangeWidth, completion: nil)
}

the error I'm getting is in the start of the block line:

Cannot convert the expression's type '() -> () -> $T0' to type '() -> () -> $T0'

I think it still has something to do with defining the (weak) cell.

Community
  • 1
  • 1
SwingerDinger
  • 276
  • 1
  • 7
  • 21

1 Answers1

2

Similar to the post you linked, invalidate the layout then setup a closure to be called as your animation block.

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
    collectionView.collectionViewLayout.invalidateLayout()
    let cell = collectionView.cellForItemAtIndexPath(indexPath)!
    let animateChangeWidth: ()-> Void  = {[weak cell] in
        if let validCell = cell {
            var frame = validCell.frame
            frame.size = validCell.viewWithTag(100)!.intrinsicContentSize()
            validCell.frame = frame
        }

    }
    UIView.transitionWithView(cell, duration: 0.7, options: UIViewAnimationOptions.CurveLinear, animations: animateChangeWidth, completion: nil)
}
JMFR
  • 799
  • 5
  • 18
  • Thanks for the fast reaction, I just don't understand what you mean with the `weak cell`? And do you still use `;` inside a closure? – SwingerDinger Oct 29 '14 at 13:35
  • The `[weak cell]` thing is a closure [Capture List](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-XID_103) to avoid a retain cycle. I put that in there because the other guy was using a weak reference to the `cell` so I imagined that we better use it here too. (edited out the semicolons, they are not needed, copy and paste error) – JMFR Oct 29 '14 at 18:50
  • Still not working @JMFR . It gives me a weird error telling me it cannot convert the expression's type. See update. Seems this problem is out of my league – SwingerDinger Oct 29 '14 at 19:37
  • No Xcode right now. You might try leaving off the capture list. Delete the `[weak cell] in` part? Or maybe declaring a type for our Closure, should be `() -> Void` – JMFR Oct 29 '14 at 20:13
  • 1
    I did get the code to work and to fire the animation, but I am not a collection view expert either. You can checkout the code at [https://github.com/regnerjr/CollectionViewTest](https://github.com/regnerjr/CollectionViewTest) a simple collection view with images which zoom when they are selected. The problem is the other views in the collection do not move out of the way when the selected image zooms. Feel free to reach out if you want more help working though this. – JMFR Oct 30 '14 at 02:21
  • I have tried this code. But it gives error in the line "frame.size = validCell.viewWithTag(100)!.intrinsicContentSize()" . The error is "fatal error: unexpectedly found nil while unwrapping an Optional value " – PRADIP KUMAR Jun 24 '16 at 15:12
  • o yeah I got you.You have taken the image viewWithTag as 100 either dynamically or thru storyboard – PRADIP KUMAR Jun 24 '16 at 15:22