簡體   English   中英

UICollectionView選擇時放大單元格

[英]UICollectionView Enlarge cell on selection

選擇集合視圖時,我想調整其大小。 我從以前的帖子中找到了解決方法,但是遇到了問題。 我在調試時發現選擇落后了一個,但是我不確定為什么。

例如,如果我通過點擊選擇一個單元格,則什么也不會發生。 如果我之后選擇另一個單元格,則我選擇的第一個單元格將被放大。 如果選擇第三個單元格,則第二個單元格將被放大。 等等。

這就是我實現它的方式,並且只有一次像我想要的那樣一次擴大了單元格:

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)

請注意以下短語: 取消選擇 ItemAtIndexPath。

Select ItemAtIndexPath是一種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM