//
// EditRecipeTableViewController.swift
// Customer
//
// Created by Aleksandr Govorukhin on 11.06.17.
// Copyright © 2017 Aleksandr Govorukhin. All rights reserved.
//
import UIKit
import SugarRecord
class EditRecipeTableViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var imageCollectionRecipe: UICollectionView!
@IBOutlet weak var textEditName: UITextView!
@IBOutlet weak var textEditDescription: UITextView!
@IBOutlet weak var textEditSourceLink: UITextView!
var recipe: Recipe!
override func viewDidLoad() {
super.viewDidLoad()
textEditName.text = recipe.nameRecipe
textEditDescription.text = recipe.descriptionRecipe
textEditSourceLink.text = recipe.sourceLink
self.imageCollectionRecipe.dataSource = self
self.imageCollectionRecipe.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let images = NSKeyedUnarchiver.unarchiveObject(with: recipe.imageRecipe! as Data) as! NSMutableArray
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Collection_cell", for: indexPath) as! CollectionViewCell
let images = NSKeyedUnarchiver.unarchiveObject(with: recipe.imageRecipe! as Data) as! NSMutableArray
let img = UIImage(data: images[indexPath.row] as! Data)!
cell.recipeImageView.image = img
return cell
}
func goToListOfRecepies() {
performSegue(withIdentifier: "backToListOfRecepies", sender: self)
}
@IBAction func saveEditRecipe() {
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let images = NSKeyedUnarchiver.unarchiveObject(with: recipe.imageRecipe! as Data) as! NSMutableArray
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Collection_cell", for: indexPath) as! CollectionViewCell
let images = NSKeyedUnarchiver.unarchiveObject(with: recipe.imageRecipe! as Data) as! NSMutableArray
let img = UIImage(data: images[indexPath.row] as! Data)!
cell.recipeImageView.image = img
return cell
}
func nextPage(index: Int) {
if let nextVC = self.showViewControllerAtIndex(index: index) {
setViewControllers([nextVC], direction: .forward, animated: true, completion: nil)
}
}
@IBAction func nextPage(sender: AnyObject) {
index += 1
let pageViewController = self.parent as! PageViewController
pageViewController.nextPage(index: index)
}
var page = 1
func nextPage(index: Int) {
if let nextVC = self.showViewControllerAtIndex(index: page) {
setViewControllers([nextVC], direction: .forward, animated: true, completion: nil)
page += 1
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
constructionYear.text = answerYears[row]
} else if pickerView.tag == 0 {
contractYear.text = answerYears[row]
}
}