I have a TableViewController
class that implements UITableViewController
and NSFetchedResultsController
protocols.
On selecting a row
, the app is designed to segue to a detail screen. However, when I first select any row
other than the first, the data in the cell
changes to show the default values as defined in the storyboard.
As far as I can determine, there is no code in my app that does this. And when I step through the code in debug mode, I am unable to discover any code that is triggered when the row
is selected.
Consequently, I am unsure what parts of my app's code to include to help identify what might be going on.
Any suggestions on where I go from here would be much valued.
EDIT ----
I've been asked to post my code. As I have no idea which code is triggering, here's the class definition for the TVC which implements the UITableViewController and NSFetchedResultsControllerDelegate protocol. I've only included the overall class definition, class properties and two methods initializeFetchedResultsController and ConfigureCell.
I don't know if this limited code sample is going to be enough as I am unable to determine what is being called when the user touches the row at runtime.
I'm happy to include more if there are any ideas.
class PlayerTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
var players: [NSManagedObject] = []
var fetchedResultsController: NSFetchedResultsController<NSFetchRequestResult>!
var managedObjectContext: NSManagedObjectContext? = (UIApplication.shared.delegate as? AppDelegate)?.managedObjectContext
func initializeFetchedResultsController() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Golfer")
let firstNameSort = NSSortDescriptor(key: "firstName", ascending: true)
let lastNameSort = NSSortDescriptor(key: "lastName", ascending: true)
request.sortDescriptors = [firstNameSort, lastNameSort]
let moc = appDelegate.managedObjectContext
fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: "firstName", cacheName: nil)
fetchedResultsController.delegate = self
do {
try fetchedResultsController.performFetch()
} catch {
fatalError("Failed to initialize FetchedResultsController: \(error)")
}
}
func configureCell(cell: UITableViewCell, indexPath: NSIndexPath) {
var handicap: Float
let cellIdentifier = "PlayerTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath as IndexPath) as? PlayerTableViewCell else {
fatalError("The dequeued cell is not an instance of PlayerTableViewCell")
}
// Populate cell from the NSManagedObject instance
guard let player = fetchedResultsController.object(at: indexPath as IndexPath) as? Golfer else {
fatalError("Unexpected Object in FetchedResultsController")
}
let firstName = player.value(forKey: "firstName") as! String?
let lastName = player.value(forKey: "lastName") as! String?
let fullName = firstName! + " " + lastName!
handicap = player.value(forKey: "exactHandicap") as! Float
cell.playerFullName?.text = fullName
cell.playerPhoto.image = UIImage(data: player.value(forKey: "photo") as! Data)
cell.numExactHandicap.text = "\(Int(handicap))"
cell.numStrokesReceived.text = "\(handicap.cleanValue)"
}
}