ios 在没有 UITableViewController 的情况下拉动刷新 UITableView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10291537/
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
Pull to refresh UITableView without UITableViewController
提问by Daniel Robinson
I'm trying to implement a pull to refresh feature in a UITableView within a UIViewController. I can't use a UITableViewController because I want the UITableView to be a smaller subview in the view controller, with some other stuff above it. I assume this is possible, but has anyone seen an implementation of it?
我正在尝试在 UIViewController 中的 UITableView 中实现拉动刷新功能。我不能使用 UITableViewController 因为我希望 UITableView 是视图控制器中的一个较小的子视图,在它上面有一些其他的东西。我认为这是可能的,但是有人见过它的实现吗?
回答by Berik
Add a refresh control directly to a UITableView
without using a UITableViewController
:
UITableView
不使用 a直接向 a 添加刷新控件UITableViewController
:
override func viewDidLoad() {
super.viewDidLoad()
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refresh(_:)), for: .valueChanged)
if #available(iOS 10.0, *) {
tableView.refreshControl = refreshControl
} else {
tableView.backgroundView = refreshControl
}
}
@objc func refresh(_ refreshControl: UIRefreshControl) {
// Do your job, when done:
refreshControl.endRefreshing()
}
回答by Manish Methani
Objective-C:
目标-C:
This is how you can implement pull to refresh for table view. Same as in the case of collection view. Just replace table view alloc with collection view.
这就是你可以如何实现表格视图的拉动刷新。与集合视图的情况相同。只需将表视图 alloc 替换为集合视图。
UITableView *tableViewDemo = [[UITableView alloc]init];
tableViewDemo.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
tableViewDemo.dataSource = self;
tableViewDemo.delegate = self;
[self.view addSubView: tableViewDemo];
UIRefreshControl *refreshController = [[UIRefreshControl alloc] init];
[refreshController addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[tableViewDemo addSubview:refreshController];
#pragma mark - Handle Refresh Method
-(void)handleRefresh : (id)sender
{
NSLog (@"Pull To Refresh Method Called");
[refreshController endRefreshing];
}
回答by Andres
This solution from @berik works fine but the UIController is displayed on top of the UITableViewController. The way to fix it is doing this change:
@berik 的这个解决方案工作正常,但 UIController 显示在 UITableViewController 之上。修复它的方法是进行此更改:
override func viewDidLoad() {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: "refresh:", forControlEvents: .ValueChanged)
tableView.backgroundView = refreshControl // <- THIS!!!
}
func refresh(refreshControl: UIRefreshControl) {
// Do your job, when done:
refreshControl.endRefreshing()
}
回答by Juan Alberto López Cavallotti
I've implemented EGORefreshTableHeaderView with a UIViewController and a simple table view, the trick is that a in the places where EGO takes a scroll view as a parameter, if you look the table view itself inherits from scroll view.
我已经用 UIViewController 和一个简单的表视图实现了 EGORefreshTableHeaderView,诀窍是在 EGO 将滚动视图作为参数的地方,如果你看表视图本身继承自滚动视图。
It only requires that and a few extra connections :)
它只需要那个和一些额外的连接:)
Hope this helps.
希望这可以帮助。
回答by Giannis Giannopoulos
It seems that if you create the UIRefreshControl inside the viewController's loadViewmethod everything works fine. The UIRefreshControl behaves as it should. Tested with iOS 7.1 and iOS 8.2
似乎如果您在 viewController 的loadView方法中创建 UIRefreshControl一切正常。UIRefreshControl 行为正常。在 iOS 7.1 和 iOS 8.2 上测试
回答by Alex Bin Zhao
I ended up using ODRefreshControl.
It doesn't need any hack like the above tableView.backgroundView = refreshControl
, works almost the same way, and gives a better looking UI.
我最终使用了ODRefreshControl。它不需要像上面那样的任何 hack tableView.backgroundView = refreshControl
,工作方式几乎相同,并提供更好看的 UI。