Skip to content

Instantly share code, notes, and snippets.

@nicnocquee
Created October 19, 2018 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicnocquee/ddfb639b84ddc9053158bdee14c934ba to your computer and use it in GitHub Desktop.
Save nicnocquee/ddfb639b84ddc9053158bdee14c934ba to your computer and use it in GitHub Desktop.
Testing UICollectionView in Swift Playground iPad
import UIKit
import PlaygroundSupport
let cellSize = CGSize(width: 100, height: 100)
class Cell: UICollectionViewCell {
var imageView: UIImageView?
var identifier: String?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initSubview(frame: CGRect(origin: .zero, size: cellSize))
}
override init(frame: CGRect) {
super.init(frame: frame)
self.initSubview(frame: CGRect(origin: .zero, size: cellSize))
}
override func prepareForReuse() {
self.identifier = nil
self.imageView?.image = nil
}
func initSubview(frame: CGRect) {
self.backgroundColor = .yellow
if self.imageView == nil {
self.contentView.backgroundColor = .gray
self.contentView.frame = CGRect(origin: .zero, size: cellSize)
self.imageView = UIImageView(frame: CGRect(origin: CGPoint.zero, size: frame.size))
self.contentView.addSubview(self.imageView!)
self.imageView?.clipsToBounds = true
self.imageView?.contentMode = .scaleAspectFill
self.imageView?.backgroundColor = .gray
self.imageView?.isOpaque = true
}
}
}
class MyViewController: UICollectionViewController {
var allAssets: [String] = []
override init(collectionViewLayout layout: UICollectionViewLayout) {
super.init(collectionViewLayout: layout)
self.collectionView?.backgroundColor = .white
self.collectionView?.register(Cell.self, forCellWithReuseIdentifier: "cell")
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Loading photos ..."
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
for i in 0...900 {
self.allAssets.append("\(i)")
}
self.collectionView?.reloadData()
self.title = "\(self.allAssets.count) items"
}
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (self.allAssets.count) ?? 0
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! Cell
let asset = self.allAssets[indexPath.row]
cell.identifier = asset
return cell
}
}
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 10
layout.minimumLineSpacing = 10
layout.itemSize = cellSize
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
PlaygroundPage.current.liveView = UINavigationController(rootViewController: MyViewController(collectionViewLayout: layout))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment