Table view is not getting updated after dismissing the popover?
Asked Answered
S

1

1

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.

Sommelier answered 9/5, 2016 at 15:11 Comment(5)
Which reload do you use? When you call it are you in the main thread? Does cellForRow.. get called? When it does, do you return the right cell? Need way more info to help youLyndonlyndsay
In the Team table view controller's viewWillAppear method I'm fetching the data from the coredata. In the To Add teams's submit method(which is a button i tap upon giving the values in the popover). The popover gets dismissed but the tableview still not getting updated with the latest values.Could you get me what I'm trying to say.Sommelier
I recommend converting your code to use a fetched results controller (as you do for Members), and implement the delegate methods to update the table view.Meliamelic
Is this necessary?! Whether there is any other way to achieve this?Sommelier
@PraveenKumar If you don't want to swap to FRC, I would use a delegate/protocol pattern: I'll add an answer....Meliamelic
M
3

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
    
Meliamelic answered 9/5, 2016 at 17:14 Comment(1)
I did it through NSNotificationCenter, anyway thanks for the help :)Sommelier

© 2022 - 2024 — McMap. All rights reserved.