xcode 当我回到视图控制器时刷新 tableview 顶部的控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38184255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh Control on top of tableview when I go back to view controller
提问by jplozano
For some reason, the first time the view controller is loaded, the refresh control behaves as expected (that it, showing under tableview when swipe down). The problem is when I tap on another tab, and then go back to the view controller, the refresh control does not fade in anymore and is visible on top of the tableview.
出于某种原因,第一次加载视图控制器时,刷新控件的行为符合预期(向下滑动时显示在 tableview 下)。问题是当我点击另一个选项卡,然后返回到视图控制器时,刷新控件不再淡入并且在 tableview 顶部可见。
This is part of the code:
这是代码的一部分:
class CoreTableViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableView:UITableView!
var tableData:Array<AnyObject> = []
var dataFetched:Bool = false
var refreshControl:UIRefreshControl?
override func viewDidLoad() {
super.viewDidLoad()
self.edgesForExtendedLayout = UIRectEdge.None;
self.tableView = self.assignTableView()
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None;
if self.enableRefresher() {
self.setRefreshControl()
}
}
func enableRefresher() -> Bool {
return true
}
func fetchData(cleanFirst:Bool = false) {
// Fetch data.
}
func refresh() {
self.fetchData(true)
self.refreshControl!.endRefreshing()
}
func setRefreshControl() {
self.refreshControl = UIRefreshControl()
//self.refreshControl!.attributedTitle = NSAttributedString(string: "Actualizar")
self.refreshControl!.addTarget(self, action: #selector(CoreTableViewController.refresh), forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refreshControl!)
}
}
Any thoughts?
有什么想法吗?
回答by jplozano
I found the fix here: https://stackoverflow.com/a/29088409/209200
我在这里找到了修复:https: //stackoverflow.com/a/29088409/209200
Instead of self.tableView.addSubview(refreshControl!)
, it should be self.tableView.insertSubview(refreshControl!, atIndex: 0)
.
相反self.tableView.addSubview(refreshControl!)
,它应该是self.tableView.insertSubview(refreshControl!, atIndex: 0)
。
That was causing the refres control to appear over the tableview.
这导致 refres 控件出现在 tableview 上。
回答by Ankit Kumar Gupta
Try this,
尝试这个,
The Above solutions are fine but tableView.refreshControl is available for UITableViewController only, till iOS 9.x and comes in UITableView from iOS 10.x onwards.
上述解决方案很好,但 tableView.refreshControl 仅适用于 UITableViewController,直到 iOS 9.x 并且从 iOS 10.x 开始出现在 UITableView 中。
To fix this Just use :
要解决这个问题,只需使用:
Written in Swift 3 -
用 Swift 3 编写 -
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(FeedViewController.loadNewData), for: UIControlEvents.valueChanged)
// Fix for the RefreshControl Not appearing in background
tableView?.addSubview(refreshControl)
tableView.sendSubview(toBack: refreshControl)
回答by Victor John
Try to set z index as -1 which means UIRefreshControl is behind any view.
尝试将 z 索引设置为 -1,这意味着 UIRefreshControl 位于任何视图之后。
refreshControl.layer.zPosition = -1
and
和
tableView.refreshControl?.beginRefreshing()
self.tableView.refreshControl?.endRefreshing()
Both must use in pair.
两者必须成对使用。
回答by Dima Deplov
As I examine your code and your question, I think the problem is here:
当我检查您的代码和您的问题时,我认为问题出在这里:
"In addition to assigning a refresh control to a table view controller's refreshControl
property, you must configure the target and action of the control itself." https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIRefreshControl_class/index.html#//apple_ref/occ/instm/UIRefreshControl/endRefreshing
“除了将刷新控件分配给表视图控制器的refreshControl
属性之外,您还必须配置控件本身的目标和操作。” https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIRefreshControl_class/index.html#//apple_ref/occ/instm/UIRefreshControl/endRefreshing
So, from documentation, you need to use UITableViewController, not UIViewController. And set it's refreshControl property.
因此,根据文档,您需要使用 UITableViewController,而不是 UIViewController。并设置它的 refreshControl 属性。
In the current state, you just add UIRefreshControl as regular subview.
在当前状态下,您只需将 UIRefreshControl 添加为常规子视图。
回答by jandro_es
It looks ok, I suspect it's because some race condition regarding the creation of the control itself. I found out that using lazy properties helps a lot in this cases. I'll do something like this:
看起来没问题,我怀疑这是因为在创建控件本身时存在一些竞争条件。我发现在这种情况下使用惰性属性有很大帮助。我会做这样的事情:
lazy var refreshControl: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action:#selector(CoreTableViewController.refresh(_:)), forControlEvents: .ValueChanged)
return refreshControl
}()
override public func viewDidLoad() {
super.viewDidLoad()
if self.enableRefresher() {
tableView.addSubview(refreshControl)
}
........
}
func refresh(refreshControl: UIRefreshControl) {
self.fetchData(true)
refreshControl.endRefreshing()
}
You don't need to use a TableViewController, an UIViewController is enough and more often than not more flexible.
您不需要使用 TableViewController,一个 UIViewController 就足够了,而且通常更灵活。
Hope it helps
希望能帮助到你
回答by Vladimir Vlasov
Unfortunately, no one solution above has helped me.
不幸的是,上述任何一种解决方案都没有帮助过我。
In my case, a refresh control appears after reloading a table view when the table view is empty. Maybe if the table view has at least one row it simply hides the refresh control, I haven't checked it. Also, when my view controller appears again it works like a workaround and it hides the refresh control.
在我的情况下,当表视图为空时重新加载表视图后会出现刷新控件。也许如果表视图至少有一行它只是隐藏了刷新控件,我还没有检查它。此外,当我的视图控制器再次出现时,它就像一种解决方法,它隐藏了刷新控件。
But I've found another workaround to hide the refresh control immediately. An async
call is mandatory here.
但我找到了另一种解决方法来立即隐藏刷新控件。一个async
电话是强制性这里。
tableView.reloadData()
DispatchQueue.main.async {
refreshControl.endRefreshing()
}
I was inspired by thisanswer in another post.
我在另一篇文章中受到了这个答案的启发。
回答by Andrej Ja??o
to me worked using insertion at 0th index of table view and intializing like lazy var:
对我来说,在表视图的第 0 个索引处插入并像惰性变量一样初始化:
private lazy var refreshControl: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.tintColor = .gray
refreshControl.addTarget(self, action: #selector(refreshFollowingList(_:)), for: .valueChanged)
return refreshControl
}()
func setupRefresher() {
self.tableView.insertSubview(refreshControl, at: 0)
}
override func viewDidLoad() {
super.viewDidLoad()
setupRefresher()
}
In case I just assigned to the tableviews refresh control the lazy var it was jumping a at the 109 pixel of content offset by 20 pixels so I fixed it like this. I am using a UIViewController not a UITableViewController, hope this helps
如果我只是将惰性变量分配给 tableviews 刷新控件,它会在 109 像素的内容偏移 20 像素处跳转,所以我像这样修复了它。我使用的是 UIViewController 而不是 UITableViewController,希望这会有所帮助
回答by Dilip Jangid
We have to do a simple configuration to assign UIRefreshControl to tableView as given below.
我们必须做一个简单的配置来将 UIRefreshControl 分配给 tableView,如下所示。
@IBOutlet weak var tableView : UITableView! {
didSet {
tableView.dataSource = self
tableView.delegate = self
tableView.refreshControl = UIRefreshControl()
tableView.refreshControl?.addTarget(self, action: #selector(handleRefresh(_:)), for: UIControl.Event.valueChanged)
}
}
Now add the handleRefresh method to handle your logic
现在添加 handleRefresh 方法来处理您的逻辑
@objc
func handleRefresh(_ refreshControl: UIRefreshControl) {
// Here i am disabling the user interaction while refreshing
tableView.alpha = 0.5
self.view.isUserInteractionEnabled = false
// Get your work done here
// At the end stop the refresh control and enable user interaction again.
tableView.refreshControl?.endRefreshing()
tableView.alpha = 1.0
self.view.isUserInteractionEnabled = true
}
Now when you come back from another tab, the refresh control stop spinning. So to handle that add the below lines of code...
现在,当您从另一个选项卡返回时,刷新控件将停止旋转。因此,要处理添加以下代码行...
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Here we are forcefully end refreshing and again starting, as we know it works when we scroll down the table. We are doing it programmatically.
if tableView.refreshControl!.isRefreshing {
let contentOffset = tableView.contentOffset
tableView.refreshControl?.endRefreshing()
tableView.refreshControl?.beginRefreshing()
tableView.contentOffset = contentOffset
}
}
Hope it will help the peoples :-)
希望它能帮助人们:-)