Skip to content

Instantly share code, notes, and snippets.

@myobie
Last active July 11, 2020 21:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/75b394b693a39d83c975 to your computer and use it in GitHub Desktop.
Save myobie/75b394b693a39d83c975 to your computer and use it in GitHub Desktop.
How to setup a custom UIWindow, UICollectionViewController, UICollectionView, and UICollectionViewCell in a iOS9 swift project
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var controller = Controller()
override init() {
super.init()
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window!.rootViewController = Controller()
window!.makeKeyAndVisible()
return true
}
}
class Controller: UICollectionViewController {
var numbers = [1,2,3,4]
init() {
super.init(collectionViewLayout: UICollectionViewFlowLayout())
}
override func loadView() {
super.loadView()
self.collectionView = ListView() // delegate and data source are set automatically
}
override func viewDidLoad() {
view.backgroundColor = UIColor.whiteColor()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numbers.count
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CellIdentifier, forIndexPath: indexPath) as! Cell
configureCell(cell, indexPath: indexPath)
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(collectionView.frame.size.width, 44)
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let total = numbers[indexPath.row] + numbers.last!
self.numbers = []
for i in 1...total {
self.numbers.append(i)
}
dispatch_async(dispatch_get_main_queue()) {
self.collectionView!.reloadData()
}
}
func configureCell(cell: Cell, indexPath: NSIndexPath) {
let message = numbers[indexPath.row].description
cell.text.text = message
}
}
let CellIdentifier = "converstaionCell"
class ListView: UICollectionView {
init() {
super.init(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
registerClass(Cell.self, forCellWithReuseIdentifier: CellIdentifier)
self.backgroundColor = UIColor.clearColor()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class Cell: UICollectionViewCell {
let text: UILabel
override init(frame: CGRect) {
self.text = UILabel(frame: CGRect(x: 10, y: 5, width: frame.size.width - 20, height: 24))
text.font = UIFont.systemFontOfSize(17.0)
text.textAlignment = .Left
text.textColor = UIColor.blackColor()
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
contentView.addSubview(text)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment