mir-aclewhip
mir-aclewhip

Reputation: 127

How to refresh data in a tableview from a different tableview

I'm working on a game where you can "hire" developers to work for you. Basically I have 1 table view that holds all the possible employees you can hire, and another table view that holds the employees you have hired.

The issue is I have a third table view where you can "learn" different coding languages, which in turn change the amount of money each developer can earn.

What I'm trying to accomplish is when the player presses a button to learn a new coding language, to update a variable for the most recent coding language that has been learned, in turn effecting the amount of money made by the developer.

I've gotten everything to work, BUT in order to update all the labels I have, I need to reload the data in the Employees table view when the "learn" button is pressed in the other table view. Any suggestions??

Upvotes: 1

Views: 586

Answers (3)

catalandres
catalandres

Reputation: 1159

Use delegation.

Assume you have two classes, EmployeeTable and LearnTable. Make the Employee table a delegate of the Learn table, via a protocol.

  • In the LearnTable class, add the delegate protocol on top:

    protocol LearnTableDelegate {
    
        func didUpdateTable()
    
    }
    
  • The EmployeeTable class must comply with the protocol and implement the function that it specifies. It's a good practice to comply with a protocol through an extension to the class:

    extension EmployeeTable: LearnTableDelegate {
    
        func didUpdateTable() {
            // do whatever is necessary to update the table
        }
    
    }
    
  • The LearnTable class needs a place to store the reference to its delegate, which will be optional, because the reference may not be set:

    class LearnTable {
    
        var delegate: LearnTableDelegate?
    
    }
    
  • (Here I am assuming you are doing this from EmployeeTable.) After you create the instance of LearnTable and before you segue into in, add the reference to the EmployeeTable as a delegate.

    let learnTable = LearnTable()
    learnTable.delegate = self
    

Upvotes: 6

Sam0711er
Sam0711er

Reputation: 894

I would suggest you go ahead and trigger an IBAction with the learn button which would then reload the other UITableView using tableView.reloadData(). If you need to trigger this throughout different ViewControllers, I would go ahead with NSNotificationCenter.

Upvotes: 1

Echizzle
Echizzle

Reputation: 3459

Do you have any code to show us?

Try using NSNotificationCenter to send a notification to the other view controller. For example,

In the class that you're calling reload from add this

NSNotificationCenter.defaultCenter().postNotificationName("reloadTableView" object: self)

In the class of the Table View you want to refresh, add this in view did load

NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "refresh",
name: "reloadTableView",
object: nil)

Then in that same class have a function called "refresh",

func refresh() {
  self.tableView.reloadData()
}

Upvotes: 3

Related Questions