UIRefreshControl is not calling its selector. Here's the hierarchy of UITableView:
UINavigationController-UITabBarController-UIViewController-UITableView
Below is Code.
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[contestTableView setContentOffset:CGPointMake(0, -self.refreshControl.frame.size.height) animated:YES];
[contestTableView addSubview:refreshControl];
[refreshControl layoutIfNeeded];
[refreshControl beginRefreshing];
-(void)handleRefresh:(id)sender
{
NSLog (#"Pull To Refresh Method Called");
[self getDataFromServer];
[self.refreshControl endRefreshing];
}
refresh control is not calling handleRefresh function on pull down of UITableview.
Kindly help.
Try commenting below line form your code. Since you have already fired beginRefreshing the refresh operation in viewDidLoad and not returning the control to its default state using endRefreshing(), the selector handleRefresh: will not be fired.
[refreshControl beginRefreshing];
beginRefreshing():
Tells the control that a refresh operation was started programmatically. And should be called only when an external event source triggers a programmatic refresh of your scrolling view, for example you update tableview periodically using timer.
You need to add the refresh control first try the sample code
swift:
if #available(iOS 10.0, *) {
tableView.refreshControl = refreshControl
} else {
tableView.addSubview(refreshControl)
}
obj-c :
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(refresh1:) forControlEvents:UIControlEventValueChanged];
[yourTableview addSubview:refreshControl];
Related
I'm just trying to simply add a UIRefreshControl to a UIScrollview- for some reason when I pull down on the scroller, nothing happens at all. No ActivityView, no action. It just bounces back up like nothing happened.
this is what I have so far:
- (void)viewDidLoad {
[super viewDidLoad];
[scroller setAutoresizesSubviews:YES];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[self.scroller addSubview:refreshControl];
//populate the scroller with data
[self grabData];
}
-(void)handleRefresh:(UIRefreshControl *)refresh {
// Reload my data
[self grabData];
}
This one should be helpfull
UIRefreshControl *refreshControl;
- (void)viewDidLoad {
[super viewDidLoad];
refreshControl = [[UIRefreshControl alloc]init];
[self.scrollView addSubview:refreshControl];
[refreshControl addTarget:self action:#selector(refreshData) forControlEvents:UIControlEventValueChanged];
}
- (void)refreshData {
//TODO: reload your data and end refreshing
[refreshControl endRefreshing];
}
if you are getting some data, after you have complete your receiving data then you should call endRefreshingand reloadData
What worked for me is make the refreshControl a variable of the class and then use it.
Eg:
class className: UIViewController{
var refreshControl:UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl = UIRefreshControl()
self.refreshControl.addTarget(self, action: Selector("getPrint"), forControlEvents: UIControlEvents.ValueChanged)
refreshControl.tintColor = UIColor.whiteColor()
self.refreshControl.backgroundColor = UIColor.clearColor()
self.refreshControl?.endRefreshing()
self.scrollView.addSubview(self.refreshControl)
}
}
I'm using pull to refresh with UICollectionView that has an header(i don't know if the header is relevant) . When i'm pull to refresh right before the refresh part the collection view jumps down(not smoothly at all).
What could be the problem?
here is my code -
- (void)addRefresh
{
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:#selector(reset) forControlEvents:UIControlEventValueChanged];
[self.refreshControl setBackgroundColor:[UIColor whiteColor]];
[self.profileCollectionView addSubview:self.refreshControl];
}
- (void)reset
{
[self.refreshControl endRefreshing];
}
I had a similar but not exactly the same problem with UIRefreshControl where the text would overlap the UIActivityIndicator of the UIRefreshControl. This would only happen the first time you would activate the UIRefreshControl. I'm not sure if this fix will help you but you can give it a try:
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//Fix for bug where spinner would overlap the text
dispatch_async(dispatch_get_main_queue(), ^{
[_refreshControl beginRefreshing];
[_refreshControl endRefreshing];
});
}
I am using UIRefreshControl in my table view. I have initiated refresh control in viewDidLoad:
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
[self.bestDealsTableView addSubview:self.refreshControl];
I have a target method for scrolling:
-(void) handleRefresh:(UIRefreshControl *) refreshControl{
[self performSelector:#selector(updateDealsList) withObject:nil withObject:nil];
}
updateDealsList method:
- (void) updateDealsList {
self.dealsService = [[BestDealsService alloc] initServiceWithDelegate:self isLocalCall:NO];
[self.dealsService fetchBestDeals];
}
In my service response method:
[self.refreshControl endRefreshing];
self.isTableViewInRefreshMode = YES;
[self.bestDealsTableView reloadData];
Now, I have two issues:
1. My refresh Control is not hiding on success response from my service.
2. If I pull table view down, I can see a refresh control, but if I scroll down again, I see a new one below the previous one.
Image for first refresh control:
Here, after scrolling down again:
Note: I am using custom TableViewCell
This is how I fixed it:
// Pull To Refresh Code
if(self.refreshControl == nil){
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
}
[self.myTableView addSubview:self.refreshControl];
Initialize your UIRefreshControl with:
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[self.bestDealsTableView addSubview:self.refreshControl];
and be sure to call [self.refreshControl endRefreshing];
tableView.contentOffset = CGPointMake(0, -100);
Im try the pull down to refresh in collection view but im use the (tableview pull down to refresh controller)same code for collection view. Please any one help my problem.
Firstly you can write this code in your viewDidLoad :-
- (void) viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.tintColor = [UIColor blackColor];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to Refresh"];
[refreshControl addTarget:self action:#selector(refreshControlAction) forControlEvents:UIControlEventValueChanged];
[collectionView addSubview:refreshControl];
}
Now add a target
- (void) refreshControlAction
{
// Enter your code for request you are creating here when you pull the collectionView. When the request is completed then the collectionView go to its original position.
}
I have a UIRefreshControl in my ViewController and a method refreshView to handle "pull to refresh" event. It works perfectly when actually pulling, but when I call [refresh beginRefreshing] in code, it just shows refresh animation but doesn't call the refreshView method.
Here's code for initializing refresh control in viewDidload:
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to Refresh"];
[refresh addTarget:self
action:#selector(refreshView:)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
Based on my understanding on the documentation: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html
Tells the control that a refresh operation was started
programmatically. ... Call this method when an external event
source triggers a programmatic refresh of your table.
I think it is not used to start a refresh operation, rather it is just for updating the refresh control status that it is currently refreshing, in which it will make the refresh control spinning. The purpose will be to avoid the user pulling the table view and trigger the refresh operation again while it is still refreshing.
So you should call the refreshView: method by yourself.
Just call beginRefreshing asynchronously.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
dispatch_async(dispatch_get_main_queue(), ^{
[refreshControl beginRefreshing];
});
//...
}
Try this
ViewDidLoad
//initialise the refresh controller
refreshControl = [[UIRefreshControl alloc] init];
//set the title for pull request
refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:#"pull to Refresh"];
//call he refresh function
[refreshControl addTarget:self action:#selector(refreshMyTableView)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
Method
-(void)refreshMyTableView{
//set the title while refreshing
refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:#"Refreshing the TableView"];
//set the date and time of refreshing
NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
[formattedDate setDateFormat:#"MMM d, h:mm a"];
NSString *lastupdated = [NSString stringWithFormat:#"Last Updated on %#",[formattedDate stringFromDate:[NSDate date]]];
refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:lastupdated];
//end the refreshing
}
to stop it
[refreshControl endRefreshing];