Guest User

Untitled

a guest
May 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3.  
  4. let fetchRequest: NSFetchRequest<Events> = Events.fetchRequest()
  5.  
  6. let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  7.  
  8. fetchRequest.sortDescriptors = [sortDescriptor]
  9.  
  10.  
  11. if let context = (UIApplication.shared.delegate as? AppDelegate)?.coreDataStack.persistentContainer.viewContext {
  12.  
  13.  
  14. fetchResaultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: "date", cacheName: nil)
  15.  
  16. fetchResaultsController.delegate = self
  17.  
  18. do {
  19. try fetchResaultsController?.performFetch()
  20. nameEvents = fetchResaultsController.fetchedObjects!
  21. print("success")
  22. } catch let error as NSError {
  23. print(error.localizedDescription)
  24. }
  25. }
  26.  
  27. func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
  28. tableViewEvents.beginUpdates()
  29. }
  30.  
  31. func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
  32.  
  33. switch type {
  34.  
  35. case .insert: guard let indexPath = newIndexPath else {break}
  36. tableViewEvents.insertRows(at: [indexPath], with: .automatic)
  37.  
  38. case .delete: guard let indexPath = indexPath else {break}
  39. tableViewEvents.deleteRows(at: [indexPath], with: .automatic)
  40.  
  41.  
  42. case .update: guard let indexPath = indexPath else {break}
  43. tableViewEvents.reloadRows(at: [indexPath], with: .automatic)
  44.  
  45. default:
  46. tableViewEvents.reloadData()
  47. }
  48.  
  49. nameEvents = controller.fetchedObjects as! [Events]
  50. }
  51.  
  52. func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
  53. switch type {
  54. case .insert:
  55. tableViewEvents.insertSections(IndexSet(integer: sectionIndex), with: .fade)
  56. case .delete:
  57. tableViewEvents.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
  58. case .move:
  59. break
  60. case .update:
  61. break
  62. }
  63. }
  64.  
  65. func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
  66. tableViewEvents.endUpdates()
  67. }
  68.  
  69.  
  70.  
  71. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  72. let sectionInfo = fetchResaultsController.sections![section]
  73. return sectionInfo.numberOfObjects
  74. }
  75.  
  76. func numberOfSections(in tableView: UITableView) -> Int {
  77. return fetchResaultsController.sections!.count }
  78.  
  79. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  80.  
  81.  
  82. let cell = tableViewEvents.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EventTableViewCell
  83.  
  84. cell.descriptionCell.text = nameEvents[indexPath.row].descript
  85. cell.photoEvent.image = UIImage(data: nameEvents[indexPath.row].photo!)
  86.  
  87.  
  88. return cell
  89. }
  90.  
  91.  
  92. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  93. let theSection = fetchResaultsController.sections![section].name
  94.  
  95. let formatter = DateFormatter()
  96. formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZ"
  97. let date = formatter.date(from: theSection)
  98. //formatter.dateFormat = "MMMM yyyy"
  99.  
  100. var formattedDateStr: String? = nil
  101. if let aDate = date {
  102. formattedDateStr = formatter.string(from: aDate)
  103. }
  104. return formattedDateStr
Add Comment
Please, Sign In to add comment