Adding UIRefreshControl to UIScrollView - ios

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

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

UICollectionView with header pull to refresh jumps

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

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

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