Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active September 17, 2016 00:46
Show Gist options
  • Save KentarouKanno/6dca588a379c26740923564bc619a4ac to your computer and use it in GitHub Desktop.
Save KentarouKanno/6dca588a379c26740923564bc619a4ac to your computer and use it in GitHub Desktop.
UICollectionView

UICollectionView

★ UICollectionViewでCellを自由に動かせるLibrary
UICollectionViews Now Have Easy Reordering

★ セル押下時にIndexPathからセルを取得する

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    
    // IndexPathからセルを取得する
    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! CustomCell
}

★ 現在画面に見えているセルを取得する

let visibleCells = collectionView.visibleCells()

★ 現在表示中のセルでFrame全てが表示されているセルを抽出する

let perfectVisibleCells = collectionView.visibleCells().filter {
    return CGRectContainsRect(collectionView.bounds, $0.frame)
}

★ セルが画面に表示される、非表示になるタイミングでセルに処理をする

// 表示されるタイミング
override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    if let cell = cell as? CustomCell {
        // do something
        
    }
}

// 非表示になるタイミング
override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    if let cell = cell as? CustomCell {
        // do something
        
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment