简体   繁体   English

在tableview.reloadData之后调用一次函数

[英]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.

相关问题 在主线程上调用tableView.reloadData后,UITableView不刷新 - UITableView not refreshing after calling tableView.reloadData on main thread tableView.reloadData()仅调用一次 - tableView.reloadData() only called once 通用函数不会在tableView.reloadData()之后更新tableview单元格中的label的值 - Generic function does not update the value of label in tableview cell after tableView.reloadData() 调用tableView.reloadData()时,“致命错误”表明tableView已正确连接 - “fatal error” when calling tableView.reloadData(), the tableView is properly connected 在调用tableView.reloadData()之后,TableView无法重新加载 - TableView fail to reload after `tableView.reloadData()` is called 为什么在我保存数据数据并调用 tableview.reloadData() 函数后我的 tabe 视图不会重新加载 - Why wont my tabe view reload after i save data data and call the tableview.reloadData() function 调用tableView.reloadData()删除自定义单元内动画 - Calling tableView.reloadData() removes custom in-cell animation Swift 3 /如何在全局函数中调用tableView.reloadData()? - Swift 3 / How to call tableView.reloadData() in global function? TableView.reloadData()无法正常工作? (迅速) - TableView.reloadData() is not working ? (SWIFT) 方法 tableView.reloadData() 不起作用 - Method tableView.reloadData() not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM