Skip to content

Instantly share code, notes, and snippets.

@CharlesAE
Last active October 15, 2018 03:01
Show Gist options
  • Save CharlesAE/f829b46157ccafeec5924aba25067687 to your computer and use it in GitHub Desktop.
Save CharlesAE/f829b46157ccafeec5924aba25067687 to your computer and use it in GitHub Desktop.
class TodayViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, NCWidgetProviding {
//Creating the collectionview and giving it some properties
let userCollectionView: UICollectionView = {
//The collectionView flow layout
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 1.0
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.backgroundColor = .clear
return cv
}()
//The string ReuseIdentifier which the collectionview requires
let userID = "userID"
//An array of UserModel objects
var users: [UserModel]?
//The number of cells to be displayed in the collection view
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
//The customized UserCell will be displayed
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = userCollectionView.dequeueReusableCell(withReuseIdentifier: userID, for: indexPath) as! UserCell
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
//Setting the view's background to transparent
view.backgroundColor = .clear
//Declaring the delegate and datasource to self, then adding the collectionview to the view
userCollectionView.dataSource = self
userCollectionView.delegate = self
view.addSubview(userCollectionView)
//Registering UserCell with the reuseidentifier
userCollectionView.register(UserCell.self, forCellWithReuseIdentifier: userID)
//setting the collectionview's anchor constraints, this ensures it is the exact size of the view it is attached to
userCollectionView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
userCollectionView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
userCollectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
userCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
//Setting the size of the cells
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width / 6.4, height: 80)
}
//applying margins to the collectionview's flow layout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 3, 0, 0)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
//This is where further instructions for the widget will be added
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
completionHandler(NCUpdateResult.newData)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment