Advertisement
Guest User

Untitled

a guest
Apr 29th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. //
  2. // PridatViewController.swift
  3. // zoznam knih
  4. //
  5. // Created by Patrik on 24.4.2015.
  6. // Copyright (c) 2015 Patrik Belis. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CoreData
  11.  
  12. public class PridatViewController: UITableViewController,UITableViewDataSource, UITableViewDelegate {
  13.  
  14.  
  15. override public func viewDidLoad() {
  16. super.viewDidLoad()
  17.  
  18. self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: Selector("saveTapped")), animated: true)
  19.  
  20. }
  21.  
  22. public override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  23. return 5
  24. }
  25.  
  26. public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  27.  
  28. let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
  29. let obsah: NSManagedObjectContext = appDel.managedObjectContext!
  30. let entity = NSEntityDescription.entityForName("List", inManagedObjectContext: obsah)
  31. var pridat = Model(entity: entity! , insertIntoManagedObjectContext: obsah)
  32.  
  33. var cell = tableView.dequeueReusableCellWithIdentifier("Cell" , forIndexPath : indexPath) as? TableViewCell
  34. if cell == nil
  35. {
  36. cell = TableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
  37. }
  38.  
  39. if (indexPath.row == 0) {
  40. cell!.configure("Názov knihy")
  41.  
  42. }
  43. if (indexPath.row == 1) {
  44. cell!.configure("Meno autora")
  45. }
  46. if (indexPath.row == 2) {
  47. cell!.configure("Rok vydania")
  48. }
  49. if (indexPath.row == 3) {
  50. cell!.configure("Vydavateľstvo")
  51. }
  52. if (indexPath.row == 4) {
  53. cell!.configure("Počet strán knihy")
  54. }
  55.  
  56. func saveTapped(){
  57. if (cell!.textField.text.isEmpty) {
  58. let alert = UIAlertView()
  59. alert.title = "Nevyplnené údaje"
  60. alert.message = "Musíš vyplniť všetky údaje o knihe."
  61. alert.addButtonWithTitle("Ok")
  62. alert.show()
  63.  
  64.  
  65. }
  66. let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
  67. let obsah: NSManagedObjectContext = appDel.managedObjectContext!
  68. let entity = NSEntityDescription.entityForName("List", inManagedObjectContext: obsah)
  69. var pridat = Model(entity: entity! , insertIntoManagedObjectContext: obsah)
  70.  
  71. pridat.kniha = cell!.textField.text
  72. pridat.autor = cell!.textField.text
  73. pridat.rok = cell!.textField.text
  74. pridat.vydavatelstvo = cell!.textField.text
  75. pridat.strany = cell!.textField.text
  76.  
  77. obsah.save(nil)
  78. }
  79.  
  80.  
  81. return cell!
  82. }
  83.  
  84.  
  85. public override func didReceiveMemoryWarning() {
  86. super.didReceiveMemoryWarning()
  87. // Dispose of any resources that can be recreated.
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement