UIRefreshControl not working when called prorgammatically - ios

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];

Related

UIRefreshControl Selector not calling objective c

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];

UIRefreshControl is not hiding after refresh. iOS

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);

UIRefreshControl not showing indicator or title?

I want to implement Pull To Refresh to my UITableViewController by using UIRefreshControl. Here what i tried so far.
- (void)viewDidLoad
{
.....
////********* Pull to refresh ************/
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to Refresh"];
refresh.tintColor = [UIColor magentaColor];
// [self.tableView addSubview:refresh];
// [self.refreshControl setEnabled:YES];
[refresh addTarget:self action:#selector(pullToRefresh:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
}
-(void)pullToRefresh:(UIRefreshControl *)refresh {
[refresh beginRefreshing];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Refreshing.."];
[self loadRecentActivities];
}
But when i pull the tableview neither activity indicator nor title is visible, however pullToRefresh is called and table refreshed.My app supports iOS7.0+.
What i'm missing? Any help would be appreciated?
Edit: I'v tried with [self.refreshControl setEnabled:YES]; and [self.refreshControl setEnabled:YES]; as mentioned in my edited code, but still no luck.
This is how my tableview looks like when i pull it to refresh-
Final Solution: For table view background i was using
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
self.tableView.backgroundView = imageView;
Refresh indicator and title was hidden behind the self.tableView.backgroundView instead use
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
and it solves my problem. also many thanks to Balram Tiwari please see his answer if you still have problem.
Just add self.tableView.backgroundView.layer.zPosition -= 1; under the initialisation code for refreshControl.
Actually you have your refresh controller there in the tableView, but it is behind the navigationBar. (Sounds different, right), Here is the catch,
When you are using code in iOS7, by default the UIViewController takes the (edgesForExtendedLayout) whole screen. That means your (0,0) will start from the top left corner, underneath the navigationBar.
So your tableView is actually starting from there. So to get rid of it, you just specify the edgesForExtendedLayout for your viewController in your viewDidLoad Method.
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
After that it should be visible.
Before above Fix:
After the Fix.
hope that helps.

Loading data on UITableView up scroll

I would like to show activityindicator and load more content from server when scroll goes out of UITableView.
I saw it in several News Feed applications:
Does anyone know this, please help me, thanks!
Use a UIRefreshControl
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"News Feed";
self.allNewsEntries = [NSMutableArray array];
[self downloadNewsEntries];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(refreshContent)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
}
-(void) refreshContent {
[self.allNewsEntries removeAllObjects];
[self downloadNewsEntries];
[self.refreshControl endRefreshing];
}

Is it possible to do pull down to refresh and load more in customized uicollectioncell in uicollectionview?

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.
}

Resources