Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // robotTableViewController.swift
- // aiSonar
- //
- // Created by Ayad on 12/11/18.
- // Copyright © 2018 sonar.io. All rights reserved.
- //
- import UIKit
- import CoreData
- class RobotTableViewController: UITableViewController {
- @IBOutlet weak var cerrarSesion: UIBarButtonItem!
- var listaRobot = [Robot](){
- didSet{
- self.tableView.reloadData()
- }
- }
- @IBOutlet weak var saveBoton: UIBarButtonItem!
- @IBAction func cerrarSesion(sender: UIBarButtonItem) {
- let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
- let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
- self.presentViewController(nextViewController, animated:true, completion:nil)
- }
- // MARK: View lifecycle functions
- override func viewDidLoad() {
- super.viewDidLoad()
- title = "Robots"
- let textAttributes = [NSForegroundColorAttributeName: UIColor(red: 40/255, green: 175/255, blue: 176/255, alpha: 1)]
- navigationController?.navigationBar.titleTextAttributes = textAttributes
- cerrarSesion.title = "Logout"
- cerrarSesion.tintColor = UIColor.whiteColor()
- cerrarSesion.setBackgroundImage(UIImage(named: "botonAnadir.png"), forState: .Normal, style: .Plain, barMetrics: .Default)
- //cerrarSesion.setTitlePositionAdjustment(UIOffset(horizontal: 2, vertical: 0), forBarMetrics: .Default)
- self.navigationItem.leftBarButtonItem = cerrarSesion
- saveBoton.tintColor = UIColor.whiteColor()
- saveBoton.setBackgroundImage(UIImage(named: "botonAnadir.png"), forState: .Normal, barMetrics: .Default)
- let backgroundImage = UIImage(named: "radar.jpg")
- let imageView = UIImageView(image: backgroundImage)
- imageView.contentMode = .ScaleAspectFit
- self.tableView.backgroundView = imageView
- tableView.backgroundColor = UIColor.lightGrayColor()
- tableView.tableFooterView = UIView(frame: CGRectZero)
- }
- override func viewWillAppear(animated: Bool) {
- super.viewWillAppear(animated)
- self.tabBarController?.tabBar.hidden = false
- let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
- let managedContext = appDelegate.managedObjectContext
- let fetchRequest = NSFetchRequest(entityName: "User")
- let loggedUsername = NSUserDefaults.standardUserDefaults().valueForKey("username") as! String
- let predicate = NSPredicate(format: "username == %@", loggedUsername)
- fetchRequest.predicate = predicate
- do {
- let loggedUser = (try managedContext.executeFetchRequest(fetchRequest) as! [User]).first!
- self.listaRobot = loggedUser.mutableSetValueForKey("robots").allObjects as! [Robot]
- } catch {
- print("Error while fetching logged user")
- }
- }
- // MARK: Table view definition
- override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return listaRobot.count
- }
- override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
- return 1
- }
- override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
- let person = listaRobot[indexPath.row]
- let cell = tableView.dequeueReusableCellWithIdentifier("RobotTableViewCell", forIndexPath: indexPath) as! RobotTableViewCell
- let dataImg: NSData = person.valueForKeyPath("image") as! NSData
- cell.foto.image = UIImage(data: dataImg)
- cell.nombre.text = person.valueForKeyPath("name") as? String
- cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
- return cell
- }
- override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
- if editingStyle == .Delete {
- guard let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate else {
- return
- }
- let managedContext = appDelegate.managedObjectContext
- managedContext.deleteObject(listaRobot[indexPath.row])
- do{
- try managedContext.save()
- listaRobot.removeAtIndex(indexPath.row)
- self.tableView.reloadData()
- }catch{
- print("Error")
- }
- }
- }
- override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
- cell.backgroundColor = UIColor(white: 1, alpha: 0.5)
- }
- // MARK: Navigation
- override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
- let volver = UIBarButtonItem()
- volver.title = "< Volver"
- volver.setBackButtonBackgroundImage(UIImage(named: "botonEditar.png"), forState: .Normal, barMetrics: .Default)
- navigationItem.backBarButtonItem = volver
- if segue.identifier == "robotDetail" {
- let selectedCell = sender as! RobotTableViewCell
- let destinationVC = segue.destinationViewController as! RobotDetailsViewController
- let selectedRow = tableView.indexPathForCell(selectedCell)
- destinationVC.robot = listaRobot[(selectedRow?.row)!]
- }
- }
- }
- class RobotTableViewCell: UITableViewCell {
- @IBOutlet weak var foto: UIImageView!
- @IBOutlet weak var nombre: UILabel!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement