问题
I'm banging my head for some time due to this issue. I precise my scenario in detail.
I have a table view where I can add data using a popover which gets displayed on clicking the '+' button in the navigation bar. I get the values from the popover but where I'm stuck is, the data received is not getting reflected in the tableview. If I move back and forth it gets displayed. Tried to reload the table with different possibilities but nothing works.
If you do want a taste of my code, you can get it here Data stored fails to display in the table view, in one to many relationship of core data?
Could anyone solve my problem, help is very much appreciated.
回答1:
The idea here is to provide a way for the Add Teams popover view controller to tell the Team table view controller to reload its table view.
In the Add Team VC swift file, define a protocol:
protocol AddTeamsDelegateProtocol { func didAddTeam() }
In the Add Team class, add a new
delegate
property which of this type:var delegate : AddTeamsDelegateProtocol? = nil
In the same class, call the delegate method when the new Team is saved:
@IBAction func submit(sender: AnyObject) { let entity = NSEntityDescription.entityForName("Teams", inManagedObjectContext: managedObjectContext) let team = Teams(entity: entity!, insertIntoManagedObjectContext: managedObjectContext) team.teamName = teamNamePO.text team.teamImage = teamImagePO.image do{ try managedObjectContext.save() } catch let error as NSError{ print("\(error), \(error.userInfo)") } self.delegate?.didAddTeam() dismissViewControllerAnimated(true, completion: nil) }
In the Team table view controller, implement the
didAddTeam()
method:func didAddTeam() { let request = NSFetchRequest(entityName: "Teams") do{ teamData = try managedObjectContext.executeFetchRequest(request) as! [Teams] } catch let error as NSError { print("\(error), \(error.userInfo)") } self.tableView.reloadData() }
Ensure that the Team table view controller conforms to the protocol
class GroupTable: UITableViewController, NSFetchedResultsControllerDelegate, AddTeamsDelegateProtocol {
Before segueing to (or presenting) the Add Teams popover (I couldn't see how this is done in your code in the other question), set the Add Teams controller's delegate:
addTeamsVC.delegate = self
来源:https://stackoverflow.com/questions/37119298/table-view-is-not-getting-updated-after-dismissing-the-popover