daily pastebin goal
69%
SHARE
TWEET

Untitled

a guest Jan 16th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  CocktailTableViewController.swift
  3. //  UdacityCocktailApp
  4. //
  5. //  Created by Kynan Song on 06/01/2019.
  6. //  Copyright © 2019 Kynan Song. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CoreData
  11.  
  12. class CocktailTableViewController: UIViewController {
  13.  
  14.     @IBOutlet var cocktailList: UITableView!
  15.    
  16.     var fetchResultController : NSFetchedResultsController<Cocktail>!
  17.     var cocktailNameToPass:String!
  18.     var cocktailInformationToPass:String!
  19.     var cocktailMeasurementsToPass:String!
  20.     var ingredientsReference: NSSet?
  21.    
  22.     var selectedIndexes = [IndexPath]()
  23.     var insertedIndexPaths: [IndexPath]!
  24.     var deletedIndexPaths: [IndexPath]!
  25.     var updatedIndexPaths: [IndexPath]!
  26.     var allCocktails:[Cocktail]!
  27.     override func viewDidLoad() {
  28.         super.viewDidLoad()
  29.         cocktailList.dataSource = self
  30.         cocktailList.delegate = self
  31.         fetchCocktailsList()
  32.         cocktailList.reloadData()
  33.        
  34.     }
  35.    
  36.     override func viewWillAppear(_ animated: Bool) {
  37.         super.viewWillAppear(animated)
  38.         fetchCocktailsList()
  39.         cocktailList.reloadData()
  40.         if let indexPath = cocktailList.indexPathForSelectedRow {
  41.             cocktailList.deselectRow(at: indexPath, animated: false)
  42.             cocktailList.reloadRows(at: [indexPath], with: .fade)
  43.         }
  44.     }
  45.    
  46.     func fetchCocktailsList() {
  47.        
  48.         //Needs to change to draw from joining table.
  49.         //https://stackoverflow.com/questions/15062860/fetching-data-using-join-table-in-coredata
  50.        
  51.         let fetchRequest: NSFetchRequest<Cocktail> = Cocktail.fetchRequest()
  52.         let primarySortDescriptor = NSSortDescriptor(key: "name", ascending: true)
  53.         fetchRequest.sortDescriptors = [primarySortDescriptor]
  54.        
  55.         let referencePredicate = NSPredicate(format: "ingredientReference == %@", ingredientsReference!)
  56.          allCocktails = (try! DataManager.sharedInstance().context.fetch(fetchRequest as! NSFetchRequest<NSFetchRequestResult>)) as! [Cocktail]
  57.    
  58.         for cocktail in allCocktails {
  59.             let filteredCocktails = cocktail.ingredientReference?.filtered(using: referencePredicate)
  60.             if (filteredCocktails?.count)! > 0 {
  61.                 let listObject = filteredCocktails
  62.                 print("Ingredient Name: \(cocktail.name)", terminator: "")
  63.             }
  64.         }
  65.        
  66.         fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: DataManager.sharedInstance().context, sectionNameKeyPath: nil, cacheName: "cocktails")
  67.         fetchResultController.delegate = self
  68.        
  69.        
  70.         var error: NSError?
  71.         do {
  72.             try self.fetchResultController.performFetch()
  73.         } catch let fetchError as NSError {
  74.             error = fetchError
  75.         }
  76.         //cocktailList.reloadData()
  77.        
  78.         if let error = error {
  79.             print("\(error)")
  80.         }
  81.        
  82.     }
  83.    
  84. }
  85.     // MARK: - Table view data source
  86. extension CocktailTableViewController: UITableViewDelegate, UITableViewDataSource {
  87.  
  88.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  89.         //let arrayCount = fetchResultController.fetchedObjects
  90.         return allCocktails!.count
  91.     }
  92.    
  93.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  94.         let cell = tableView.dequeueReusableCell(withIdentifier: "cocktailCell", for: indexPath)
  95.         let cocktail = allCocktails![indexPath.row]
  96.         cell.textLabel?.text = cocktail.name
  97.         return cell
  98.     }
  99.    
  100.     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  101.         let cell = cocktailList.cellForRow(at: indexPath)
  102.         let cocktail = allCocktails[indexPath.row]
  103.         cocktailNameToPass = cocktail.name
  104.         cocktailMeasurementsToPass = cocktail.measurments
  105.         cocktailInformationToPass = cocktail.information
  106.         print("tapped: \(cocktail.name)")
  107.         performSegue(withIdentifier: "cocktailInformationSegue", sender: cell)
  108.     }
  109.    
  110.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  111.         if(segue.identifier == "cocktailInformationSegue") {
  112.             let viewController = segue.destination as! CocktailInformationViewController
  113.             viewController.sentName = cocktailNameToPass
  114.             viewController.sentInformation = cocktailInformationToPass
  115.             viewController.sentMeasurements = cocktailMeasurementsToPass
  116.         }
  117.     }
  118.  
  119.  
  120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top