[英]Calling a function once after tableview.reloadData
Every time I reload my table by calling 每次我通过电话重新加载我的桌子
tableview.reloadData()
I want to call a specification function like 我想调用一个规范函数
myFunction()
I was wondering, instead of stacking these two function next one after another everywhere in my code like 我想知道,而不是在我的代码中一个接一个地堆叠这两个函数
tableview.reloadData()
myFunction()
Is there a smart and clean way of calling myFunction every time tableview reloads? 每次tableview重新加载时,是否有一种智能和干净的方法来调用myFunction?
There is no delegate method to give you a callback when reloadData() has completed, but to make it cleaner you could do a couple of different things. 当reloadData()完成时,没有委托方法可以给你一个回调,但为了使它更干净,你可以做几件不同的事情。
You could create your own function like this: 您可以像这样创建自己的函数:
func reloadTable() {
tableView.reloadData()
myFunction()
//plus anything else you want to accomplish
}
Then you call that function everywhere in one line instead of repeating your code. 然后在一行中到处调用该函数,而不是重复代码。
Alternatively, you could subclass UITableView
and override the reloadData() method, adding your additional functionality. 或者,您可以
UITableView
并覆盖reloadData()方法,添加其他功能。
one way is to use inheritance. 一种方法是使用继承。 just implement your own tableview class and reload the reloadData function of UITableView.
只需实现自己的tableview类并重新加载UITableView的reloadData函数。
class YourTableView: UITableView {
override func reloadData() {
super.reloadData()
myFunction()
}
func myFunction() {
//do something
}
}
then declare YourTableView instead of UITableView 然后声明YourTableView而不是UITableView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.