I have this code when I tap on the cell, the link sent me to a view controller where I can edit my data. This code usually work well, when I use a UItableViewController, but this time, I used a viewController where I attach a custom tableView with static cell and an other tableView with a Dynamic Prototype cell. so I connect the cell with my ViewController and I name the identifier "edit". But for some reason doesn't matter what I tried, I can not make the view to change when I press on the cell. I am out of idea, someone as some idea?
I gave a sample of the code, but I think it is more an Xcode problem?!
import UIKit import CoreData class ViewControllerTVC: UIViewController, NSFetchedResultsControllerDelegate, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableViewD: UITableView! var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext var frc = NSFetchedResultsController<NSFetchRequestResult>() func getFetchedResultsController() -> NSFetchedResultsController<NSFetchRequestResult>{ frc = NSFetchedResultsController(fetchRequest: listFetchRequest(), managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil) return frc } func listFetchRequest() -> NSFetchRequest<NSFetchRequestResult>{ let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "AA") let sortDescriptor = NSSortDescriptor(key:"arm", ascending: true) fetchRequest.sortDescriptors = [sortDescriptor] return fetchRequest } override func viewDidLoad() { super.viewDidLoad() print("-------- Opening the table view ------------") //++ let appDel = UIApplication.shared.delegate as! AppDelegate let context = appDel.persistentContainer.viewContext let request = NSFetchRequest<NSFetchRequestResult> (entityName: "AA") request.returnsObjectsAsFaults = false; let results:NSArray = try! context.fetch(request) as NSArray if results.count > 0 { print("AA \(results.count) found!") }else{ print("Setting no Results Found") } //+ setupTableView() frc = getFetchedResultsController() frc.delegate = self do { try frc.performFetch() print("Perform Fetch") } catch _ { } tableViewD.reloadData() } func setupTableView() { tableViewD.delegate = self tableViewD.dataSource = self tableViewD.register(UITableViewCell.self, forCellReuseIdentifier: "cell") } // MARK: - Table view data source private func controllerDidChangeContent(controller: NSFetchedResultsController<NSFetchRequestResult>) { tableViewD.reloadData() } func numberOfSectionsInTableView(tableView: UITableView) -> Int { let numberOfSections = frc.sections?.count return numberOfSections! } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let numberOfRowsInSection = frc.sections?[section].numberOfObjects return numberOfRowsInSection! } func tableView(_ tableView: UITableView, cellForRowAt IndexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: IndexPath as IndexPath) let list = frc.object(at: IndexPath) as! AA // // let Dec = list.minutelat print("Loading Data") cell.textLabel?.text = list.arm // cell.detailTextLabel?.text = "Really Loading" return cell } // Override to support editing the table view. private func tableView(_ tableView: UITableView, commitEditingStyle editingStyle: UITableViewCell.EditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { let managedObject:NSManagedObject = frc.object(at: indexPath as IndexPath) as! NSManagedObject context.delete(managedObject) tableView.reloadData() do { try context.save() } catch _ { } } override func prepare(for segue: UIStoryboardSegue, sender: Any?){ print("Segue should open view!!!!!!!!!!!!") if segue.identifier == "edit1" { let cell = sender as! UITableViewCell let indexPath = tableViewD.indexPath(for: cell) let taskController:ViewController = segue.destination as! ViewController let nItem:AA = frc.object(at: indexPath!) as! AA taskController.nItem = nItem } } } https://stackoverflow.com/questions/67428114/cell-connection-to-a-view-controller-not-working May 07, 2021 at 10:04AM
没有评论:
发表评论