ios - UICollectionView 放大选择的单元格

标签 ios swift uicollectionview

我想在选择 Collection View 中的特定单元格时调整其大小。我从之前的帖子中找到了如何做到这一点,但遇到了问题。我在调试时发现选择落后了一位,但我不确定为什么。

例如,如果我通过点击选择一个单元格,则不会发生任何情况。如果我之后选择另一个单元格,那么我选择的第一个单元格就会放大。如果我选择第三个单元格,则第二个单元格会放大。等等。

这就是我实现它的方式,只有当单元格像我想要的那样同时放大时:

var selectedIndexPath: NSIndexPath!
func collectionView(collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    if selectedIndexPath != nil { //We know that we have to enlarge at least one cell
        if indexPath == selectedIndexPath {
            return CGSize(width: self.gallery.frame.size.width, height: self.gallery.frame.size.width)
        }
        else {
            return CGSize(width: self.gallery.frame.size.width/3.0, height: self.gallery.frame.size.width/3.0)
        }
    }
    else {
        return CGSize(width: self.gallery.frame.size.width/3.0, height: self.gallery.frame.size.width/3.0)
    }
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    if selectedIndexPath != nil && selectedIndexPath == indexPath
    {
        selectedIndexPath = nil //Trigger large cell set back to normal
    }
    else {
        selectedIndexPath = indexPath //User selected cell at this index
    }
    collectionView.reloadData()
}

我在调试时发现选择在 didDeselectItemAtIndexPath 中以我上面描述的方式滞后,但不确定为什么。

最佳答案

问题是您正在使用func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)

注意这句话:didDeselectItemAtIndexPath。

didSelectItemAtIndexPath 是一种可行的方法。

关于ios - UICollectionView 放大选择的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34478329/

相关文章:

ios - 开始输入时出现的 View ,iOS

ios - 使用 registerDefaults 首次启动时设置为空白

ios - 为什么 UIWebViewDelegate 分配而不是弱?

Ios 录制 Live mic 数据无法录制和发送?

ios - 如何避免共享库中的函数重定义?

ios - 'NSError' 在 Swift 中不能转换为 '@lvalue inout $T9'

ios - Swift:无法在 TableViewController ( Storyboard)上创建按钮操作

ios - 如何从 Collection View 单元格内调用 Collection View Controller

objective-c - 带有自定义项的 UIMenuController 不适用于 UICollectionview

ios - cellForItemAtIndexPath : firing in iOS7 but not iOS6