Praveen Kumar
Praveen Kumar

Reputation: 296

Table view is not getting updated after dismissing the popover?

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.

Upvotes: 1

Views: 1239

Answers (1)

pbasdf
pbasdf

Reputation: 21536

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.

  1. In the Add Team VC swift file, define a protocol:

    protocol AddTeamsDelegateProtocol {
        func didAddTeam()
    }
    
  2. In the Add Team class, add a new delegate property which of this type:

    var delegate : AddTeamsDelegateProtocol? = nil
    
  3. 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)
    }
    
  4. 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()
    }
    
  5. Ensure that the Team table view controller conforms to the protocol

    class GroupTable: UITableViewController, NSFetchedResultsControllerDelegate, AddTeamsDelegateProtocol {
    
  6. 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
    

Upvotes: 3

Related Questions