Data is not connected to tableView - swift

I'm building an app, which has three tableViews: One for clubs, which leads to another for members and this leads to transactions. The first two tableViews plus their "add" controller are working, whilst the third one isn't. The add controller works as far as I know, because I know that the transaction gets saved to Core Data. But the transaction isn't displayed in the tableView and I have no idea how I can fix this because this is my first App.
The Transaction Vc Code
import UIKit
import MessageUI
import CoreData
class TransactionViewController: UIViewController, MFMailComposeViewControllerDelegate, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableViewTransaction: UITableView!
let dateFromatter = DateFormatter()
var member: Member?
override func viewDidLoad() {
super.viewDidLoad()
dateFromatter.timeStyle = .long
dateFromatter.dateStyle = .long
}
override func viewWillAppear(_ animated: Bool) {
self.tableViewTransaction.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func addNewTransaction(_ sender: Any) {
performSegue(withIdentifier: "newTransaction", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let destination = segue.destination as? AddTransactionViewController else {
return
}
destination.member = member
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return member?.transactions?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewTransaction.dequeueReusableCell(withIdentifier: "transactionCell", for: indexPath)
if let transaction = member?.transactions?[indexPath.row] {
cell.textLabel?.text = transaction.reason
if let date = transaction.date {
cell.detailTextLabel?.text = dateFromatter.string(from: date)
}
}
return cell
}
#IBAction func sendEmail(_ sender: Any) {
let mailComposeVC = configureMailController()
if MFMailComposeViewController.canSendMail() {
self.present(mailComposeVC, animated: true, completion: nil)
} else {
showMailError()
}
}
func configureMailController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setSubject("MAHNUNG")
mailComposerVC.setMessageBody("Hey, ich habe gesehen, dass dein Kontostand in der Buchhaltung zu tief ist. Ich bitte dich daher schnellst möglich mir das Geld zu bringen. Gruss", isHTML: false)
return mailComposerVC
}
func showMailError() {
let senMailErrorAlert = UIAlertController(title: "Could not send email", message: "Dein Gerät konnte die Email nicht senden.", preferredStyle: .alert)
let dismiss = UIAlertAction(title: "Ok", style: .default, handler: nil)
senMailErrorAlert.addAction(dismiss)
self.present(senMailErrorAlert, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
The addTransaction Code:
import UIKit
class AddTransactionViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var reasonTextField: UITextField!
#IBOutlet weak var amountTextField: UITextField!
#IBOutlet weak var datePicker: UIDatePicker!
var member: Member?
override func viewDidLoad() {
super.viewDidLoad()
reasonTextField.delegate = self
amountTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
reasonTextField.resignFirstResponder()
amountTextField.resignFirstResponder()
}
#IBAction func saveTransaction(_ sender: Any) {
let reason = reasonTextField.text
let date = datePicker.date
let moneyText = amountTextField.text ?? ""
let money = Double(moneyText) ?? 0.0
if let transaction = Trancsaction(money: money, reason: reason, date: date) {
member?.addToRawTransactions(transaction)
do {
try transaction.managedObjectContext?.save()
self.navigationController?.popViewController(animated: true)
} catch {
let alert = UIAlertController(title: "Transaktion konnte nicht gespeichert werden", message: "Bitte wiederholen Sie ihre Eingabe", preferredStyle: .alert)
present(alert, animated: true, completion: nil)
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
}
Core Data:
import UIKit
import CoreData
#objc(Trancsaction)
public class Trancsaction: NSManagedObject {
var date: Date? {
get {
return rawDate as Date?
}
set {
rawDate = newValue as NSDate?
}
}
convenience init?(money: Double, reason: String?, date: Date?) {
let appDelegate = UIApplication.shared.delegate as? AppDelegate
guard let context = appDelegate?.persistentContainer.viewContext else {
return nil
}
self.init(entity: Trancsaction.entity() , insertInto: context)
self.date = date
self.money = money
self.reason = reason
}
}

Related

Error in Table View Controller When using a segue Fatal error: Index out of range

I've ran into a problem and i cant resolve it
TableViewController:
var items: [String?] = ["door","table","chair"]
var givenDescription: [String?] = ["Change the description using
class TableViewController: UITableViewController, UIToolbarDelegate {
the edit option"]
#IBOutlet var myTableView: UITableView!
override func viewDidAppear(_ animated: Bool) {
myTableView.reloadData()
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = items[indexPath.row]
cell.detailTextLabel?.text = givenDescription[indexPath.row]
return cell
}
ViewController:
import UIKit
import MobileCoreServices
class AddViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var newPic: Bool?
//Outlets
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var addName: UITextField!
#IBOutlet weak var addDescription: UITextField!
#IBAction func addImage(_ sender: Any) {
let myAlert = UIAlertController(title: "Select Image From", message: "", preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: nil)
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
self.newPic = true
}
let cameraRoll = UIAlertAction(title: "Camera Roll", style: .default, handler: nil)
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
self.newPic = false
}
myAlert.addAction(cameraAction)
myAlert.addAction(cameraRoll)
self.present(myAlert, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
if mediaType.isEqual(to: kUTTypeImage as String) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = image
if newPic == true {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(imageError), nil)
}
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
#objc func imageError(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafeRawPointer) {
if error != nil {
let alert = UIAlertController(title: "Save Failed", message: "Failed to save image", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Okay", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
}
//Action
#IBAction func create(_ sender: Any) {
if (addName.text != "") {
items.append(addName.text!)
addName.text = ""
}
if (addDescription.text != "") {
givenDescription.append(addDescription.text!)
addDescription.text = ""
}
self.navigationController?.popViewController(animated: true)
}
}
You can "fix" the create function in several ways, for example like this:
#IBAction func create(_ sender: Any) {
if (addName.text != "" && addDescription.text != "") {
items.append(addName.text!)
addName.text = ""
givenDescription.append(addDescription.text!)
addDescription.text = ""
}
self.navigationController?.popViewController(animated: true)
}
but a much better option is to create a simple struct
struct Item {
var name: String
var itemDescription: String
}
and use it instead
#IBAction func create(_ sender: Any) {
if (addName.text != "" && addDescription.text != "") { //or just check one depending on if one is more important
let item = Item(name: addName.text!, itemDescription: addDescription.text!)
items.append(item)
}
self.navigationController?.popViewController(animated: true)
}
and the items array is defined as
var items: [Item]()
andd for your table view
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
let item = items[indexPath.row]
cell.textLabel?.text = item.name
cell.detailTextLabel?.text = item.itemDescription
return cell
}
This was the answer so simple.
#IBAction func create(_ sender: Any) {
if (addName.text != "" && addDescription.text != "") {
items.append(addName.text!)
addName.text = ""
givenDescription.append(addDescription.text!)
addDescription.text = ""
}
self.navigationController?.popViewController(animated: true)
}

Swift 3 - Get value from core data

I have a class that will take 2 inputs, Sets and Reps
Using prepare(for segue: UIStoryboardSegue, sender: Any?) I passed the values back to the WorkoutViewController.Swift
The data is successfully saved since after pressing save barbutton in the SetRepsViewController.Swift the window pops
However the labels do not show the values
What am I doing wrong here
I believe that targetLog?.setAndrep?.sets is not correctly getting the value
Can someone please help me on what I should do?
Screenshow of the viewController
WorkoutViewController.swift
import UIKit
import CoreData
class WorkoutViewController: UIViewController{
var muscleLog: MuscleList?
var targetLog: Log?
#IBOutlet weak var LogTableView: UITableView!
#IBOutlet weak var input: UITextField!
#IBOutlet weak var weightInput: UITextField!
#IBOutlet weak var repsInput: UITextField!
#IBOutlet weak var dateView: UILabel!
#IBOutlet weak var setsLabel: UILabel!
#IBOutlet weak var repsLabel: UILabel!
let dateFormatter = DateFormatter()
func workoutTitle(title: String){
self.title = title
}
override func viewDidLoad() {
super.viewDidLoad()
dateFormatter.calendar = Calendar(identifier: .persian)
dateFormatter.dateFormat = "dd/MM/yyyy"
let date = Date()
let dateInPersian = dateFormatter.string(from: date)
dateView.text = dateInPersian
}
override func viewWillAppear(_ animated: Bool) {
setsLabel.text = targetLog?.repLog?.sets
repsLabel.text = targetLog?.setAndrep?.reps
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "addTarget"{
guard let destination = segue.destination as? SetRepsViewController else {
return
}
destination.destLog = targetLog
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//hide keybaord method
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
#IBAction func addLog(_ sender: Any) {
let weight = Double(weightInput.text ?? "") ?? 0.0
let reps = Int16(repsInput.text ?? "") ?? 0
let todayDate = dateView.text ?? ""
let notes = input.text ?? ""
if let log = Log(weight: weight, reps: reps, currentday: todayDate, notes: notes){
muscleLog?.addToDailyLogs(log)
do{
try log.managedObjectContext?.save()
}catch {
print("Could not Log")
}
}
LogTableView.reloadData()
}
func deleteMuscle(at indexPath: IndexPath){
guard let logs = muscleLog?.logList?[indexPath.row],
let managedContext = logs.managedObjectContext else{
return
}
managedContext.delete(logs)
do{
try managedContext.save()
LogTableView.deleteRows(at: [indexPath], with: .automatic)
}catch{
print("Could not save")
LogTableView.reloadRows(at: [indexPath], with: .automatic)
}
}
}
extension WorkoutViewController: UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return muscleLog?.logList?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let WorkCell = LogTableView.dequeueReusableCell(withIdentifier: "WorkCell", for: indexPath)
if let logs = muscleLog?.logList?[indexPath.row] {
WorkCell.textLabel?.text = logs.currentday
let weight = String(logs.weight)
let reps = String(logs.reps)
let note = logs.notes
WorkCell.detailTextLabel?.text = weight + " kg x " + reps + " reps Notes: "+note!
}
return WorkCell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
deleteMuscle(at: indexPath)
}
}
}
SetRepsViewController.swift
import UIKit
class SetRepsViewController: UIViewController {
var destLog: Log?
#IBOutlet weak var setText: UITextField!
#IBOutlet weak var repText: UITextField!
#IBAction func saveTarget(_ sender: UIBarButtonItem) {
let sets = setText.text ?? ""
let reps = repText.text ?? ""
if let target = SetRep(sets: sets, reps: reps){
destLog?.addsetAndrep(target)
do{
try target.managedObjectContext?.save()
self.navigationController?.popViewController(animated: true)
}catch{
print("Could not save workout")
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
#IMan after saving the data in core data you will have to fetch the data from CoreData then you can show your data.
You need to set the value for destLog in your prepareForSegue by determining which row has been selected in your tableView:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "addTarget"{
guard let destination = segue.destination as? SetRepsViewController else {
return
}
let indexPath = LogTableView.indexPathForSelectedRow
targetLog = muscleLog?.logList?[indexPath.row]
destination.destLog = targetLog
}
}
That will ensure that the Log corresponding to the selected row in your table view is passed to the SetRepsViewController. When you dismiss the SetRepsViewController, the viewWillAppear code will update the labels correctly as targetLog and destLog both refer to the same Log object.
#IMan Try this
func fetchList() {
let moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchRequest: NSFetchRequest<"YOUR ENTITY NAME"> = ("YOUR ENTITY NAME").fetchRequest()
do {
let fetchResults = try self.moc.fetch(fetchRequest)
for Entity in fetchResults as [NSManagedObject] {
let entity1 = Entity.value(forKey: "YOUR ENTITY KEY VALUE")
let entity12 = Entity.value(forKey: "YOUR ENTITY KEY VALUE")
print(entity1, entity2)
} catch {
print(error.localizedDescription)
}
}

CoreData not Persisting?

I am saving an exercise into core data and calling it into the table, this works in terms of carrying the info from the user input into the table, however the coredata doesnt persist so when i re open the app, the entry is lost.
It was actually working yesterday and seems to have broken, but I havent made a change that would effect this as far as im aware. The one thing i found when debugging is that when I loaded the app its meant to point at the sql database in my console, however its changed to a .configurationprofiles file? Could this be a cause and what would the fix be? I will include the code for the tableview and the code for the user entry form below to show the information flow. Let me know if any other data is needed to be added.
import Foundation
import UIKit
import CoreData
class ExerciseEditorController: UIViewController, UITextFieldDelegate {
var managedObjectContext: NSManagedObjectContext?
var userRepsCount = Int()
var userSetsCount = Int()
#IBOutlet weak var userExerciseName: UITextField!
#IBOutlet weak var userExerciseSetCounter: UILabel!
#IBOutlet weak var userExerciseRepsCounter: UILabel!
#IBOutlet weak var userExerciseWeight: UITextField!
#IBAction func userSetsStepper(_ sender: UIStepper) {
userExerciseSetCounter.text = Int(sender.value).description
self.userSetsCount = Int(sender.value)
}
#IBAction func userRepsStepper(_ sender: UIStepper) {
userExerciseRepsCounter.text = Int(sender.value).description
self.userRepsCount = Int(sender.value)
}
#IBAction func cancelExerciseEditor(_ sender: Any) {
self.performSegue(withIdentifier: "unwindToWorkoutDesignerWithSegue:", sender: self)
}
#IBAction func saveExerciseToWorkout(_ sender: Any) {
createExercise()
self.performSegue(withIdentifier: "unwindToWorkoutDesignerWithSegue:", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = (UIColor.customBackgroundGraphite())
userExerciseSetCounter.text = String(userSetsCount)
userExerciseRepsCounter.text = String(userSetsCount)
userExerciseWeight.delegate = self
userExerciseWeight.keyboardType = .numbersAndPunctuation
}
func createExercise() {
let userExerciseWeightSet = Double(self.userExerciseWeight.text!) //make this safe!
guard let managedObjectContext = managedObjectContext else { return }
let userExercise = UserExercise(context: managedObjectContext)
userExercise.name = userExerciseName.text
userExercise.sets = Int64(userSetsCount)
userExercise.reps = Int64(userRepsCount)
userExercise.weight = userExerciseWeightSet! //make this safe!
userExercise.createdAt = Date().timeIntervalSince1970
}
func animateTextField(textField: UITextField, up: Bool) {
let movementDistance:CGFloat = -130
let movementDuration: Double = 0.3
var movement:CGFloat = 0
if up {
movement = movementDistance
}
else {
movement = -movementDistance
}
UIView.beginAnimations("animateTextField", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration)
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.animateTextField(textField: textField, up:true)
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.animateTextField(textField: textField, up:false)
}
}
And this is the tableview:
import Foundation
import UIKit
import CoreData
class WorkoutDesignerController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
#IBAction func unwindToWorkoutDesigner(segue: UIStoryboardSegue) {}
#IBOutlet weak var workoutDesignerTable: UITableView!
#IBOutlet weak var tapToAddExercise: UILabel!
#IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
#IBAction func cancelWorkoutDesigner(_ sender: Any) {
self.performSegue(withIdentifier: "unwindToTemplatesWithSegue", sender: self)
}
private let persistentContainer = NSPersistentContainer(name: "Lift")
override func viewDidLoad() {
super.viewDidLoad()
setupView()
workoutDesignerTable.delegate = self
workoutDesignerTable.dataSource = self
view.backgroundColor = (UIColor.customBackgroundGraphite())
persistentContainer.loadPersistentStores { (persistentStoreDescription, error) in
if let error = error {
print("Unable to Load Persistent Store")
print("\(error), \(error.localizedDescription)")
} else {
self.setupView()
do {
try self.fetchedResultsController.performFetch()
} catch {
let fetchError = error as NSError
print("Unable to Perform Fetch Request")
print("\(fetchError), \(fetchError.localizedDescription)")
}
self.updateView()
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let userExercises = fetchedResultsController.fetchedObjects else { return 0 }
return userExercises.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? RoutineTableViewCell else {
fatalError("Unexpected Index Path")
}
cell.backgroundColor = UIColor.customBackgroundGraphite()
cell.textLabel?.textColor = UIColor.white
let userExercise = fetchedResultsController.object(at: indexPath)
cell.nameLabel.text = userExercise.name
cell.repsLabel.text = String(userExercise.reps)
cell.setsLabel.text = String(userExercise.sets)
cell.weightLabel.text = String(userExercise.weight)
return cell
}
private func setupView() {
setupMessageLabel()
updateView()
}
private func setupMessageLabel() {
tapToAddExercise.text = "Tap + To Add An Exercise To The Routine"
}
fileprivate func updateView() {
var hasUserExercises = false
if let UserExercise = fetchedResultsController.fetchedObjects {
hasUserExercises = UserExercise.count > 0
}
workoutDesignerTable.isHidden = !hasUserExercises
tapToAddExercise.isHidden = hasUserExercises
activityIndicatorView.stopAnimating()
}
fileprivate lazy var fetchedResultsController: NSFetchedResultsController<UserExercise> = {
// Create Fetch Request
let fetchRequest: NSFetchRequest<UserExercise> = UserExercise.fetchRequest()
// Configure Fetch Request
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: true)]
// Create Fetched Results Controller
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
// Configure Fetched Results Controller
fetchedResultsController.delegate = self
return fetchedResultsController
}()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "addNewExerciseSegue" {
if let destinationViewController = segue.destination as? ExerciseEditorController {
// Configure View Controller
destinationViewController.managedObjectContext = persistentContainer.viewContext
}
}
}
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
workoutDesignerTable.beginUpdates()
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
workoutDesignerTable.endUpdates()
updateView()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch (type) {
case .insert:
if let indexPath = newIndexPath {
workoutDesignerTable.insertRows(at: [indexPath], with: .fade)
}
break;
default:
print("...")
}
}
}
You need to call context.save().

Difficulties in moving Page in swift

when I select UIButoon(selectMaghsad or selectMabda) go AircraftSearch to SelectedCity without difficulty and problem I choose one cell in selecteCity(tableviewController) go back to AircraftSearch and show data in label (labelcity) The problem is that Without that I want again go to tableViewController(selecteCity)
and alert :
A
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x7f867b5a4920 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}
why ??
Where do you think the problem is?
can you help me ??
AircraftSearch
class AircraftSearch: UIViewController ,SendbackDelegate{
#IBOutlet weak var Mabda: UIButton!
#IBOutlet weak var maghsad: UIButton!
#IBOutlet weak var labelcity: UILabel!
#IBOutlet weak var cityTectfield: UITextField!
var Airurl = NSURL()
var ScrOrDstArray = [MabdaAndMaghsad]()
var origin = [String]() // save mabda
var purpose = [String]() // save maghsad
var sendDataToTableview = [String]()
var tit = String()
override func viewDidLoad() {
super.viewDidLoad()
labelcity.text = tit
GetPassCity()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func selectMabda(sender: AnyObject) {
sendDataToTableview = origin
performSegueWithIdentifier("SelectedCellSegue", sender: sender)
}
#IBAction func selectMaghsad(sender: AnyObject) {
sendDataToTableview = purpose
print(sendDataToTableview)
performSegueWithIdentifier("SelectedCellSegue", sender: sender)
}
func originAndpurpose() {
let dataCity = ScrOrDstArray
for i in dataCity{
if i.SrcOrDst == true{
origin.append(i.Name)
}else{
purpose.append(i.Name)
}
}
}
func GetPassCity(){
let actInd : UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0,0, 50, 50)) as UIActivityIndicatorView
actInd.center = self.view.center
actInd.hidesWhenStopped = true
actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(actInd)
actInd.startAnimating()
NSURLSession.sharedSession().dataTaskWithURL(Airurl){ ( data ,response ,error) in
if error != nil{
print("A")
print(error!)
}else{
do{
//readin data from Server
let posts = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! [[String:AnyObject]]
//save data
for post in posts{
var postCity:MabdaAndMaghsad?
if let Id = post["Id"] as? Int ,
let nameCity = post["Name"] as? String ,
let SrcOrDst = post["SrcOrDst"] as? Bool
{
postCity = MabdaAndMaghsad(ID: Id, Name: nameCity, SrcOrDst: SrcOrDst)
}
self.ScrOrDstArray.append(postCity!)
}
//===============
dispatch_async(dispatch_get_main_queue()){
actInd.stopAnimating()
self.originAndpurpose()
print(self.origin)
print("=======")
// print(self.purpose)
}
}catch let error as NSError{
print("B")
print(error)
}
}
}.resume()
}
func sendNameToPreviousVC(SelectCity: String) {
tit = SelectCity
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SelectedCellSegue" {
if let VC = segue.destinationViewController as? SelectedCity {
VC.toTake = sendDataToTableview
VC.delegate = self
}
}
}
}
SelectedCity view
import UIKit
protocol SendbackDelegate:class {
func sendNameToPreviousVC(City:String)
}
class SelectedCity: UITableViewController {
var toTake = [String]()
var selecteCity = String()
weak var delegate: SendbackDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return toTake.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("stcell", forIndexPath: indexPath) as? mAndMCell
let nameCity = toTake[indexPath.row]
print(nameCity)
cell!.nameCityLabel.text = nameCity
return cell!
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as! mAndMCell!
selecteCity = currentCell.nameCityLabel!.text as String!
sendBackIdCity(selecteCity)
navigationController?.popViewControllerAnimated(true)
}
func sendBackIdCity(name: String){
self.delegate?.sendNameToPreviousVC(name)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "backCitySegue"{
var VCOne = segue.destinationViewController as? AircraftSearch
self.delegate = VCOne
}
}
}
pic

How can I get the cells to print in my ViewController2?

I am trying to get my app to display the name of the selected cell on a different screen. I got it to display the word "Sample" but I want to show the name of the person selected. What can I do for it to show? It's just the one line of code I need to change in my prepareforSegue.
ViewController:
import UIKit
import CoreData
class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
var ppl = [NSManagedObject]()
#IBAction func addName(sender: AnyObject) {
let alert = UIAlertController(title: "New Client",
message: "Add a new client",
preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save",
style: .Default,
handler: { (action:UIAlertAction) -> Void in
let textField = alert.textFields!.first
self.saveName(textField!.text!)
self.tableView.reloadData()
})
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction) -> Void in
}
alert.addTextFieldWithConfigurationHandler {
(textField: UITextField) -> Void in
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert,
animated: true,
completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Clients"
accessData()
tableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "Cell")
tableView.reloadData()
}
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return ppl.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell =
tableView.dequeueReusableCellWithIdentifier("Cell")
let person = ppl[indexPath.row]
cell!.textLabel!.text =
person.valueForKey("name") as? String
return cell!
}
func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("SELECTED ITEM")
performSegueWithIdentifier("selectedView", sender: tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func saveName(name: String) {
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Person",
inManagedObjectContext:managedContext)
let person = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext: managedContext)
person.setValue(name, forKey: "name")
do {
try managedContext.save()
ppl.append(person)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
func accessData() {
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Person")
do {
let results =
try managedContext.executeFetchRequest(fetchRequest)
ppl = results as! [NSManagedObject]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier=="selectedView") {
let dvc=segue.destinationViewController as! ViewController2
dvc.name="Sample"
//let selectedItem: NSManagedObject = ppl[self.tableView.indexPathForSelectedRow!.row] as NSManagedObject
//dvc.name = selectedItem.valueForKey("Cell") as! String
}
}
}
ViewController2:
import UIKit
class ViewController2:UIViewController{
#IBOutlet weak var userDisplay: UILabel!
var name = String()
override func viewDidLoad() {
super.viewDidLoad()
userDisplay.text = name
}
func printMe(){
print("I am a controller")
}
}
This can be done easily, instead of sending tableView as the sender when doing performSegueWithIdentifier("selectedView", sender: tableView) send the indexPath of the selected row, thus you have:
performSegueWithIdentifier("selectedView", sender: indexPath)
All you have to do then is change prepareForSegue method to be something like
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let indexPath = sender {
let cell = self.tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath)
if let name = cell?.textLabel?.text {
let dvc = segue.destinationViewController as! SecondVC
dvc.name = name
}
}
}