UITableView UIRefreshControl Does Not Show Its View The First Time
Asked Answered
L

4

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?

Lebensraum answered 19/6, 2013 at 13:49 Comment(1)
N
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.

Nyssa answered 19/10, 2013 at 4:38 Comment(5)
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];Lebensraum
Now the first cell of the tableview is almost behind the navigation bar. Can you tell me how to fix it?Lebensraum
Hi @Lebensraum , 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.Nyssa
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?Lebensraum
for people which can't seem to get it working this way: #40154514Caruthers
D
1

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

Dogvane answered 19/6, 2013 at 13:55 Comment(5)
i added [self pullToRefresh] in viewWillAppear but saw no difference in the output. Anything else i can do ?Lebensraum
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.Dogvane
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 confusionLebensraum
which spinner? the one in the Nav bar or the one below it?Dogvane
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 UIRefreshControlLebensraum
Q
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
Quartered answered 19/6, 2013 at 14:29 Comment(1)
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!Lebensraum
D
0

before the code:

[refreshControl beginRefresh]

insert the code:

[refreshControl layoutIfNeeded]

Disinclination answered 3/2, 2017 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.