UITableView UIRefreshControl Does Not Show Its View The First Time

10,542

Solution 1

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.

Solution 2

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

Solution 3

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
Share:
10,542

Related videos on Youtube

AJ112
Author by

AJ112

...

Updated on June 04, 2022

Comments

  • AJ112
    AJ112 almost 2 years

    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
    AJ112 almost 11 years
    i added [self pullToRefresh] in viewWillAppear but saw no difference in the output. Anything else i can do ?
  • AJ112
    AJ112 almost 11 years
    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!
  • Dan
    Dan almost 11 years
    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.
  • AJ112
    AJ112 almost 11 years
    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
  • Dan
    Dan almost 11 years
    which spinner? the one in the Nav bar or the one below it?
  • AJ112
    AJ112 almost 11 years
    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
    AJ112 over 10 years
    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
    AJ112 over 10 years
    Now the first cell of the tableview is almost behind the navigation bar. Can you tell me how to fix it?
  • satgi
    satgi over 10 years
    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.
  • AJ112
    AJ112 over 10 years
    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?
  • Dbl
    Dbl over 7 years
    for people which can't seem to get it working this way: stackoverflow.com/questions/40154014/…