简体   繁体   English

如何从另一个表视图刷新表视图中的数据

[英]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. 我已经完成所有工作,但是为了更新我拥有的所有标签,当在另一个表视图中按下“学习”按钮时,我需要在Employees表视图中重新加载数据。 Any suggestions?? 有什么建议么??

Use delegation . 使用委托

Assume you have two classes, EmployeeTable and LearnTable . 假设您有两个类, EmployeeTableLearnTable Make the Employee table a delegate of the Learn table, via a protocol. 通过协议使Employee表成为Learn表的委托。

  • In the LearnTable class, add the delegate protocol on top: LearnTable类中,在顶部添加委托协议:

     protocol LearnTableDelegate { func didUpdateTable() } 
  • The EmployeeTable class must comply with the protocol and implement the function that it specifies. EmployeeTable类必须符合协议并实现其指定的功能。 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: LearnTable类需要一个位置来存储对其委托的引用,这是可选的,因为可能未设置引用:

     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. (在这里,我假设您正在EmployeeTable中进行此操作。)在创建LearnTable实例LearnTable ,然后选择插入,将对EmployeeTable的引用作为委托添加。

     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. 尝试使用NSNotificationCenter将通知发送到另一个视图控制器。 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() . 我建议您继续使用学习按钮触发IBAction ,然后使用tableView.reloadData()重新加载另一个UITableView If you need to trigger this throughout different ViewControllers, I would go ahead with NSNotificationCenter . 如果您需要在不同的ViewController中触发它,我将继续使用NSNotificationCenter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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