简体   繁体   中英

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??

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 

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()
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM