Skip to content

Instantly share code, notes, and snippets.

@deepakraj27
Forked from DejanEnspyra/ViewController.swift
Created November 2, 2017 11:25
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 deepakraj27/7a3a3621b4150ec1185a4f951dfb891e to your computer and use it in GitHub Desktop.
Save deepakraj27/7a3a3621b4150ec1185a4f951dfb891e to your computer and use it in GitHub Desktop.
Example of organizing UIViewController class with the help of extensions
//
// ViewController.swift
//
// Created by Dejan Atanasov on 01/04/2017.
// Copyright © 2017 Dejan Atanasov. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
//MARK: Private Properties
@IBOutlet fileprivate weak var titleLbl: UILabel!
@IBOutlet fileprivate weak var actionBtn: UIButton!
@IBOutlet fileprivate weak var collectionView: UICollectionView!
//MARK: Internal Properties
var title: String!
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
}
//MARK: Private extension - used to store private methods
private extension ViewController{
/* do any UI related stuff here */
func setupUI(){
self.title = "Some Title"
}
/* keep your actions in this block as well */
@IBAction func someAction(btn: UIButton){
}
}
//MARK: UICollectionViewDelegate
extension ViewController: UICollectionViewDelegate{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
return cell
}
}
//MARK: UICollectionViewDataSource
extension ViewController: UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}
//MARK: UICollectionViewDelegateFlowLayout
extension ViewController: UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionView.frame.size
}
}
//MARK: SomeCustomDelegate
extension ViewController: SomeCustomDelegate{
func customDelegateMethod() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment