Table view is not getting updated after dismissing the popover?

五迷三道 提交于 2019-12-11 13:31:26

问题


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.

  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
    


来源:https://stackoverflow.com/questions/37119298/table-view-is-not-getting-updated-after-dismissing-the-popover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!