5

I have added the functionality of UIRefreshControl in my project that uses a UITableView. The app works by fetching entries from a web service to a tableview. Below is the code i have used to add UIRefreshControl:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.tintColor = [UIColor grayColor];
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Updating New Entries"];
    [refreshControl addTarget:self action:@selector(pullToRefresh) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refreshControl;

    [self pullToRefresh];    
}

- (void) pullToRefresh
{
    counter = 1;
    [self fetchEntriesNew:counter]; // My code for updating table view

    [self performSelector:@selector(updateTable) withObject:nil afterDelay:2];
}

- (void)updateTable
{
    [self.tableView reloadData];
    [self.refreshControl endRefreshing];
}

Now if i pull to refresh, it refreshes by adding new entries if there are any and shows me the following view on top of the tableview:

enter image description here

Everything works great except when the app is launched or opened for the very first time, it does not show the view that i have showed in the above image, although it does refreshes the tableview. I want it to show the refresh control view every time it refreshes it. Can anyone point out what i am doing wrong? Thanks!

UPDATE: I have added [self refreshControl beginRefreshing] and the UIRefreshControl's spinner view is now showing but its above the first entry of the tableview. Can anyone point out how to correct it?

AJ112
  • 5,291
  • 7
  • 45
  • 60
  • possible duplicate of [UIRefreshControl - beginRefreshing not working when UITableViewController is inside UINavigationController](http://stackoverflow.com/questions/14718850/uirefreshcontrol-beginrefreshing-not-working-when-uitableviewcontroller-is-ins) – JosephH Dec 20 '13 at 11:58

4 Answers4

12

This problem had really puzzled me for a while.I found that 4-inch iOS devices don't have this problem, but 3.5-inch devices do.

I tried to find out the differences between the first time that the refreshControl beginRefreshing itself and when I operated a pull gesture.It's the pull operation.

And I checked Apple's document on UIRefreshControl.It says The control does not initiate the refresh operation directly. Instead, it sends the UIControlEventValueChanged event when a refresh operation should occur.

So I thought maybe I could add something to simulate a pull gesture to trigger refreshControl's showing.

[yourScrollView(or tableView) setContentOffset:CGPointMake(0.0f, -60.0f)
                                      animated:YES];
[yourRefreshControl beginRefreshing];

It works!

PS. UIRefreshControl works with UIScrollView, too. [yourScrollView addSubview:yourRefreshControl] just works.

satgi
  • 6,353
  • 3
  • 18
  • 18
  • From your answer it looks like you are able to solve this problem. I have tried your code but it didn't work for me. I used this [self.tableView setContentOffset:CGPointMake(0.0f, 60.0f) animated:YES]; [self.refreshControl beginRefreshing]; – AJ112 Oct 19 '13 at 13:38
  • Now the first cell of the tableview is almost behind the navigation bar. Can you tell me how to fix it? – AJ112 Oct 19 '13 at 13:39
  • Hi @AJ112 , it's `-60.0f` in my code, not `60.0f`. `- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated` is meant to set the __offset__, so the offset here should be negative to simulate the __pull down__ gesture, if it's set positive, the content's top will be outside of the window. – satgi Oct 19 '13 at 13:58
  • i have added both -60 and 60 and tried adding other values but the result is exactly the same. By default its on the normal place but when its done loading, the first cell moves below the navigation bar. Is there anything else i need to do to fix it? – AJ112 Oct 19 '13 at 16:36
  • for people which can't seem to get it working this way: http://stackoverflow.com/questions/40154014/how-to-make-my-refreshcontrol-appear-during-viewdidappear – Dbl Oct 20 '16 at 12:33
1

I would move your [self pullToRefresh] call to viewWillAppear instead of viewDidLoad.

Dan
  • 5,153
  • 4
  • 31
  • 42
  • i added [self pullToRefresh] in viewWillAppear but saw no difference in the output. Anything else i can do ? – AJ112 Jun 19 '13 at 17:23
  • can you post an image of what you see that is NOT the desired outcome? Re-reading through this I just want to make sure I'm understanding the issue fully. – Dan Jun 19 '13 at 17:42
  • When the app is launched, the TableView gets refresh with new data but the spinner does not show up by itself. But if i do the pull to refresh manually, then it shows up. Hope that clears any confusion – AJ112 Jun 19 '13 at 17:45
  • which spinner? the one in the Nav bar or the one below it? – Dan Jun 19 '13 at 17:52
  • the one one the nav bar is programatically added by myself so it always shows up. I am talking about the below one that is added by UIRefreshControl – AJ112 Jun 19 '13 at 17:56
1

There are two things that can be done to add UIRefreshControl in your tableview neither of them is added in your code

1. [self setRefreshControl:tableRefreshControl];
2. [self.m_TableView addSubview:tableRefreshControl];

Either add 1 or 2 if your class is subclass of UIViewController

If your class is subclass of UITableViewController then try to replace

self.refreshControl = refreshControl; with 2 line
channi
  • 1,028
  • 9
  • 16
  • My class is a subclass of UITableViewController so as you said i have to replace self.refreshControl = refreshControl with [self.tableView addSubview:tableRefreshControl]; but what is tableRefreshControl? it does not recognize it because its declared no where and no a built in thing as well. Can you be more precise? Thanks! – AJ112 Jun 19 '13 at 17:26
0

before the code:

[refreshControl beginRefresh]

insert the code:

[refreshControl layoutIfNeeded]