Pull to refresh UITableView without UITableViewController
Objective-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.
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];
}
Add a refresh control directly to a UITableView
without using 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()
}