Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override func viewDidLoad() {
- super.viewDidLoad()
- let fetchRequest: NSFetchRequest<Events> = Events.fetchRequest()
- let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
- fetchRequest.sortDescriptors = [sortDescriptor]
- if let context = (UIApplication.shared.delegate as? AppDelegate)?.coreDataStack.persistentContainer.viewContext {
- fetchResaultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: "date", cacheName: nil)
- fetchResaultsController.delegate = self
- do {
- try fetchResaultsController?.performFetch()
- nameEvents = fetchResaultsController.fetchedObjects!
- print("success")
- } catch let error as NSError {
- print(error.localizedDescription)
- }
- }
- func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
- tableViewEvents.beginUpdates()
- }
- func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
- switch type {
- case .insert: guard let indexPath = newIndexPath else {break}
- tableViewEvents.insertRows(at: [indexPath], with: .automatic)
- case .delete: guard let indexPath = indexPath else {break}
- tableViewEvents.deleteRows(at: [indexPath], with: .automatic)
- case .update: guard let indexPath = indexPath else {break}
- tableViewEvents.reloadRows(at: [indexPath], with: .automatic)
- default:
- tableViewEvents.reloadData()
- }
- nameEvents = controller.fetchedObjects as! [Events]
- }
- func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
- switch type {
- case .insert:
- tableViewEvents.insertSections(IndexSet(integer: sectionIndex), with: .fade)
- case .delete:
- tableViewEvents.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
- case .move:
- break
- case .update:
- break
- }
- }
- func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
- tableViewEvents.endUpdates()
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- let sectionInfo = fetchResaultsController.sections![section]
- return sectionInfo.numberOfObjects
- }
- func numberOfSections(in tableView: UITableView) -> Int {
- return fetchResaultsController.sections!.count }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableViewEvents.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EventTableViewCell
- cell.descriptionCell.text = nameEvents[indexPath.row].descript
- cell.photoEvent.image = UIImage(data: nameEvents[indexPath.row].photo!)
- return cell
- }
- func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
- let theSection = fetchResaultsController.sections![section].name
- let formatter = DateFormatter()
- formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZ"
- let date = formatter.date(from: theSection)
- //formatter.dateFormat = "MMMM yyyy"
- var formattedDateStr: String? = nil
- if let aDate = date {
- formattedDateStr = formatter.string(from: aDate)
- }
- return formattedDateStr
Add Comment
Please, Sign In to add comment