Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView UIRefreshControl Does Not Show Its View The First Time

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?

like image 363
AJ112 Avatar asked Jun 19 '13 13:06

AJ112


3 Answers

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.

like image 92
satgi Avatar answered Nov 07 '22 17:11

satgi


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

like image 20
Dan Avatar answered Nov 07 '22 17:11

Dan


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
like image 1
channi Avatar answered Nov 07 '22 18:11

channi