This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
CollectionViewControllerWithSections.swift
52 lines (38 loc) · 1.94 KB
/
CollectionViewControllerWithSections.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import UIKit
import DATAStack
import CoreData
class CollectionViewControllerWithSections: UICollectionViewController {
unowned let dataStack: DATAStack
lazy var dataSource: DATASource = {
guard let collectionView = self.collectionView else { fatalError("CollectionView is nil") }
let request: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
request.sortDescriptors = [
NSSortDescriptor(key: "name", ascending: true),
NSSortDescriptor(key: "firstLetterOfName", ascending: true),
]
let dataSource = DATASource(collectionView: collectionView, cellIdentifier: CollectionCell.identifier, fetchRequest: request, mainContext: self.dataStack.mainContext, sectionName: "firstLetterOfName", configuration: { cell, item, indexPath in
let collectionCell = cell as! CollectionCell
collectionCell.textLabel.text = item.value(forKey: "name") as? String
})
return dataSource
}()
init(layout: UICollectionViewLayout, dataStack: DATAStack) {
self.dataStack = dataStack
super.init(collectionViewLayout: layout)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
guard let collectionView = self.collectionView else { fatalError("CollectionView is nil") }
collectionView.register(CollectionCell.self, forCellWithReuseIdentifier: CollectionCell.identifier)
collectionView.dataSource = self.dataSource
collectionView.backgroundColor = UIColor.white
collectionView.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(saveAction))
}
@objc func saveAction() {
Helper.addNewUser(dataStack: self.dataStack)
}
}