Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.95 KB | None | 0 0
  1. //
  2. //  robotTableViewController.swift
  3. //  aiSonar
  4. //
  5. //  Created by Ayad on 12/11/18.
  6. //  Copyright © 2018 sonar.io. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CoreData
  11.  
  12. class RobotTableViewController: UITableViewController {
  13.    
  14.    
  15.     @IBOutlet weak var cerrarSesion: UIBarButtonItem!
  16.    
  17.     var listaRobot = [Robot](){
  18.         didSet{
  19.             self.tableView.reloadData()
  20.         }
  21.     }
  22.    
  23.     @IBOutlet weak var saveBoton: UIBarButtonItem!
  24.    
  25.     @IBAction func cerrarSesion(sender: UIBarButtonItem) {
  26.         let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
  27.        
  28.         let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
  29.         self.presentViewController(nextViewController, animated:true, completion:nil)
  30.     }
  31.    
  32.    
  33.     // MARK: View lifecycle functions
  34.    
  35.     override func viewDidLoad() {
  36.         super.viewDidLoad()
  37.         title = "Robots"
  38.         let textAttributes = [NSForegroundColorAttributeName: UIColor(red: 40/255, green: 175/255, blue: 176/255, alpha: 1)]
  39.         navigationController?.navigationBar.titleTextAttributes = textAttributes
  40.        
  41.         cerrarSesion.title = "Logout"
  42.         cerrarSesion.tintColor = UIColor.whiteColor()
  43.         cerrarSesion.setBackgroundImage(UIImage(named: "botonAnadir.png"), forState: .Normal, style: .Plain, barMetrics: .Default)
  44.         //cerrarSesion.setTitlePositionAdjustment(UIOffset(horizontal: 2, vertical: 0), forBarMetrics: .Default)
  45.        
  46.         self.navigationItem.leftBarButtonItem = cerrarSesion
  47.        
  48.         saveBoton.tintColor = UIColor.whiteColor()
  49.         saveBoton.setBackgroundImage(UIImage(named: "botonAnadir.png"), forState: .Normal, barMetrics: .Default)
  50.        
  51.         let backgroundImage = UIImage(named: "radar.jpg")
  52.         let imageView = UIImageView(image: backgroundImage)
  53.         imageView.contentMode = .ScaleAspectFit
  54.         self.tableView.backgroundView = imageView
  55.        
  56.         tableView.backgroundColor = UIColor.lightGrayColor()
  57.        
  58.         tableView.tableFooterView = UIView(frame: CGRectZero)
  59.     }
  60.    
  61.    
  62.     override func viewWillAppear(animated: Bool) {
  63.         super.viewWillAppear(animated)
  64.        
  65.         self.tabBarController?.tabBar.hidden = false
  66.        
  67.         let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
  68.        
  69.         let managedContext = appDelegate.managedObjectContext
  70.        
  71.         let fetchRequest = NSFetchRequest(entityName: "User")
  72.        
  73.         let loggedUsername = NSUserDefaults.standardUserDefaults().valueForKey("username") as! String
  74.        
  75.         let predicate = NSPredicate(format: "username == %@", loggedUsername)
  76.        
  77.         fetchRequest.predicate = predicate
  78.        
  79.         do {
  80.             let loggedUser = (try managedContext.executeFetchRequest(fetchRequest) as! [User]).first!
  81.            
  82.             self.listaRobot = loggedUser.mutableSetValueForKey("robots").allObjects as! [Robot]
  83.         } catch {
  84.             print("Error while fetching logged user")
  85.         }
  86.     }
  87.    
  88.     // MARK: Table view definition
  89.    
  90.     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  91.         return listaRobot.count
  92.     }
  93.    
  94.     override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  95.         return 1
  96.     }
  97.    
  98.     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  99.        
  100.         let person = listaRobot[indexPath.row]
  101.        
  102.         let cell = tableView.dequeueReusableCellWithIdentifier("RobotTableViewCell", forIndexPath: indexPath) as! RobotTableViewCell
  103.        
  104.         let dataImg: NSData = person.valueForKeyPath("image") as! NSData
  105.        
  106.         cell.foto.image = UIImage(data: dataImg)
  107.        
  108.        
  109.         cell.nombre.text = person.valueForKeyPath("name") as? String
  110.        
  111.         cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
  112.        
  113.         return cell
  114.     }
  115.    
  116.     override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  117.         if editingStyle == .Delete {
  118.             guard let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate else {
  119.                 return
  120.             }
  121.            
  122.             let managedContext = appDelegate.managedObjectContext
  123.            
  124.             managedContext.deleteObject(listaRobot[indexPath.row])
  125.            
  126.             do{
  127.                 try managedContext.save()
  128.                 listaRobot.removeAtIndex(indexPath.row)
  129.                 self.tableView.reloadData()
  130.             }catch{
  131.                 print("Error")
  132.                
  133.             }
  134.         }
  135.     }
  136.    
  137.     override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
  138.         cell.backgroundColor = UIColor(white: 1, alpha: 0.5)
  139.     }
  140.    
  141.    
  142.    
  143.     // MARK: Navigation
  144.    
  145.     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  146.             let volver = UIBarButtonItem()
  147.             volver.title = "< Volver"
  148.             volver.setBackButtonBackgroundImage(UIImage(named: "botonEditar.png"), forState: .Normal, barMetrics: .Default)
  149.             navigationItem.backBarButtonItem = volver
  150.        
  151.         if segue.identifier == "robotDetail" {
  152.             let selectedCell = sender as! RobotTableViewCell
  153.             let destinationVC = segue.destinationViewController as! RobotDetailsViewController
  154.             let selectedRow = tableView.indexPathForCell(selectedCell)
  155.            
  156.             destinationVC.robot = listaRobot[(selectedRow?.row)!]
  157.         }
  158.     }
  159. }
  160.  
  161. class RobotTableViewCell: UITableViewCell {
  162.     @IBOutlet weak var foto: UIImageView!
  163.     @IBOutlet weak var nombre: UILabel!
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement