func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (destinationData?[indexPath.row]) != nil {
if(indexPath.row + 1 >= (destinationData?.count)!) {
expandCell(tableView: tableView, index: indexPath.row)
}
else {
if(destinationData?[indexPath.row+1] != nil) {
expandCell(tableView: tableView, index: indexPath.row)
// Close Cell (remove ExpansionCells)
} else {
contractCell(tableView: tableView, index: indexPath.row)
}
}
}
}
/* Expand cell at given index */
private func expandCell(tableView: UITableView, index: Int) {
if let infoMain = destinationData?[index]?.infoMain {
var indexesToInsert = [IndexPath]()
for i in 1...infoMain.count {
destinationData?.insert(nil, at: index + 1)
indexesToInsert.append(IndexPath(row: index + i, section: 0))
}
tableView.insertRows(at: indexesToInsert , with: .left)
tableView.scrollToRow(at: IndexPath(row: index + 1, section: 0), at: .bottom, animated: true)
}
}
/* Contract cell at given index */
private func contractCell(tableView: UITableView, index: Int) {
if let infoMain = destinationData?[index]?.infoMain {
var indexesToDelete = [IndexPath]()
for i in 1...infoMain.count {
destinationData?.remove(at: index + 1)
indexesToDelete.append(IndexPath(row: index + i, section: 0))
}
tableView.deleteRows(at: indexesToDelete, with: .left)
}
}
if Code == self.CompleteCode.text! {
performSegue(withIdentifier: "showChapter", sender: nil) //В sender можешь кинуть то, что хочешь передать другому View
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourSegue" {
let vc = segue.destination as! YourNewViewController
//Отсюда можно передать какие-либо параметры другому View
}
}
let task = session.dataTask(with: request) { (data, response, error) -> Void in
if error == nil {
let swiftyJSON = try! JSON(data: data!)
let fixtures = swiftyJSON["fixtures"].arrayValue
for element in fixtures {
let data = element["data"].stringValue
print(data) -> "2018"
}
if (result.token) {
// Пользователь успешно авторизован
} else if (result.error) {
// Пользователь отменил авторизацию или произошла ошибка
}
func textViewDidChange(_ textView: UITextView) {
let size = textView.bounds.size
let newSize = textView.sizeThatFits(CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude))
if size.height != newSize.height {
UIView.setAnimationsEnabled(false)
tableView?.beginUpdates()
tableView?.endUpdates()
UIView.setAnimationsEnabled(true)
}
}
@IBAction func saveEditRecipe() {
let CDataArray = newImages as! NSMutableArray
if let recipe = recipe {
if recipe.value(forKey: "nameRecipe") != nil {
recipe.setValue(textEditName.text, forKey: "nameRecipe")
}
if recipe.value(forKey: "descriptionRecipe") != nil {
recipe.setValue(textEditDescription.text, forKey: "descriptionRecipe")
}
if recipe.value(forKey: "imageRecipe") != nil {
recipe.setValue(NSKeyedArchiver.archivedData(withRootObject: CDataArray), forKey: "imageRecipe")
}
} else {
print("unable to fetch or create recipe")
}
(UIApplication.shared.delegate as! AppDelegate).saveContext()
self.goToListOfRecepies()
}
func parse(callback: @escaping (_ text: String, _ images: [String]) -> Void) {
let baseURL = "https://api.vk.com/method/wall.getById?posts=-34451036_490279)&access_token=5502c502caef0d55e1758dd&v=5.64"
let url = NSURL(string: baseURL)
let request = NSURLRequest(url: url! as URL)
let session = URLSession(configuration: .default)
let task = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if error == nil {
let swiftyJSON = JSON(data: data!)
let response = swiftyJSON["response"].arrayValue
var links = [String]()
for tobject in response {
let text = tobject["text"].stringValue
let attachments = tobject["attachments"].arrayValue
for photo in attachments {
let searchPhoto = photo["photo"].dictionary
if let val = searchPhoto?["photo_130"]?.stringValue {
links.append(val)
}
}
callback(text, links)
}
}
}
task.resume()
}
parse(callback: { (text, links) in
recipe.descriptionRecipe = text
(UIApplication.shared.delegate as! AppDelegate).saveContext()
self.goToHome()
})