2017-04-12 3 views
0

Ich habe ein Problem mit der Weitergabe von Daten an einen anderen View-Controller von einem Tabellenansicht-Controller. Ich versuche, die Namen der Benutzer an einen anderen View-Controller zu übergeben. Bisher arbeiten die Kerndaten, indem sie die Namen der Benutzer speichern und die Namen der Personen in den Zellen löschen, aber wenn ich einen Benutzer aus der Liste auswähle, bringt mich das nicht zum nächsten View-Controller.Übergeben Sie Daten von viewcontroller tableview zu einem anderen View-Controller

class tableViewController: UITableViewController { 

@IBOutlet weak var tableview: UITableView! 

//Properties 
var people: [NSManagedObject] = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    title = "The List" 
    tableview.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 
    // Do any additional setup after loading the view, typically from a nib. 
} 
//Fetching data from the core data 
override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    //1. Before requesting to coredata, it needs a managed object context. 
    guard let appDelegate = 
     UIApplication.shared.delegate as? AppDelegate else { 
      return 
    } 

    let managedContext = 
     appDelegate.persistentContainer.viewContext 

    //2. Using NSFetchRequest class to fetch core data. Fetching all obejcts within an entity (Person). 
    let fetchRequest = 
     NSFetchRequest<NSManagedObject>(entityName: "Person") 

    //3. Putting in the fetch request to the managed object context, to get fetch data. 
    do { 
     people = try managedContext.fetch(fetchRequest) 
    } catch let error as NSError { 
     print("Could not fetch. \(error), \(error.userInfo)") 
    } 
} 
//Adding the person's name into the table view when the clicks on the add button and enter in their name. 
@IBAction func addName(_ sender: UIBarButtonItem) { 
    let alert = UIAlertController(title: "New Name", 
            message: "Add a new name", 
            preferredStyle: .alert) 

    let saveAction = UIAlertAction(title: "Save", style: .default) { 
     [unowned self] action in 

     guard let textField = alert.textFields?.first, 
      let nameToSave = textField.text else { 
       return 
     } 

     self.save(name: nameToSave) 
     self.tableview.reloadData() 
    } 

    let cancelAction = UIAlertAction(title: "Cancel", 
            style: .default) 

    alert.addTextField() 

    alert.addAction(saveAction) 
    alert.addAction(cancelAction) 

    present(alert, animated: true) 
} 

// Where the coredata is stored. 
func save(name: String) { 

guard let appDelegate = 
    UIApplication.shared.delegate as? AppDelegate else { 
     return 
} 

//1.Before saving or retrieving from core data, NSManagedObjectContext is to be implemented. Managed context is considered s in-memory to working with managed object context. 
let managedContext = 
    appDelegate.persistentContainer.viewContext 

//2. Creating new managed object and inserting it into managed object context. NSEntityDescrption links he entity definitionfrom data model with an instance of NSanagedObject at runtime. 
let entity = 
    NSEntityDescription.entity(forEntityName: "Person", 
           in: managedContext)! 

let person = NSManagedObject(entity: entity, 
          insertInto: managedContext) 

//3. The "name" attribute is set using the key value coding. KVC key (name) should be spelt as diplayed in data model else app could crash at run time. 
person.setValue(name, forKeyPath: "name") 

//4. Changed are saved to the disk using the managed object context. Save could bring an error which is why try is used within do and catch block. The new managed object is inserted into the "people" array, so it appears when table view is reloaded. 
do { 
    try managedContext.save() 
    people.append(person) 
} catch let error as NSError { 
    print("Could not save. \(error), \(error.userInfo)") 
} 
} 
} 

// MARK: - UITableViewDataSource 
extension tableViewController: UITableViewDataSource { 
override func tableView(_ tableView: UITableView, 
       numberOfRowsInSection section: Int) -> Int { 
    return people.count 
} 

override func tableView(_ tableView: UITableView, 
       cellForRowAt indexPath: IndexPath) 
    -> UITableViewCell { 

     let person = people[indexPath.row] 
     let cell = 
      tableView.dequeueReusableCell(withIdentifier: "Cell", 
              for: indexPath) 
     cell.textLabel?.text = 
      person.value(forKeyPath: "name") as? String 
     return cell 
} 

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 
     //print("Deleted") 

     //short-cut to access App Delegate 
     let ad = UIApplication.shared.delegate as! AppDelegate 
     let context = ad.persistentContainer.viewContext 
     ad.saveContext() 

     context.delete(people[indexPath.row]) 
     people.remove(at: indexPath.row) 
     tableView.reloadData() 
    } 
} 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

} 

}

+0

Wo ist der relevante Code? –

Antwort

1

So möchten Sie die Person zur nächsten Viewcontroller ausgewählt passieren, nachdem sie von Ihrem Tableview Auswahl?

Zuerst deklarieren Sie eine Klassenvariable für die Person, die Sie auswählen.

var selectedPerson: NSManagedObject? 

dann müssen Sie die Funktion

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    selectedPerson = people[indexPath.row] 
    self.performSegue(withIdentifier: "showPerson", sender: nil) 
} 

Danach implementieren, bearbeiten Sie Ihre aktuelle Funktion dieser

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "showPerson" { 
     if let personController = segue.destination as? YOURPERSONCONTROLLERCLASS { 
      personController.person = selectedPerson 
     } 
    } 
} 

Sie werden YOURPERSONCONTROLLERCLASS zum Viewcontroller Klasse ändern müssen und dass Die Klasse benötigt außerdem eine variable Person vom Typ NSManagedObject.

+0

Vielen Dank für Ihre Hilfe, es hat funktioniert :) – Harry

+0

Wenn es nach was Sie suchen, klicken Sie auf das Häkchen-Symbol auf der linken Seite meiner Antwort, um es zu akzeptieren. Vielen Dank – Devster101

Verwandte Themen