使用Swift在selectItemAtIndexPath中显示图像

时间:2014-10-29 12:36:34

标签: swift uiimageview uicollectionview

我在每个单元格中都有collectionView个图像。 我想在点击一个单元格时看到完整的图像。

我知道可以使用selectItemAtIndexPath方法完成此操作,并且我需要对所选单元格执行某些操作:

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

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

我在objc中看过很多代码示例,比如here,但我无法在Swift中看出来。

提前谢谢


更新

到目前为止我的代码:

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)
}

我得到的错误是在块行的开头:

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

我认为它仍然与定义(弱)单元格有关。

1 个答案:

答案 0 :(得分:2)

与您链接的帖子类似,布局无效,然后设置一个闭包作为动画块调用。

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)
}