UITableView UIRefreshControl does not show its view for the first time

I added UIRefreshControl functionality in my project that uses a UITableView. The application works by retrieving records from a web service in a table format. The following is the code I used to add the 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 try to update, it updates, adding new entries, if any, and shows me the following view on top of the table:

enter image description here

Everything works fine, except when the application is launched or opened for the first time, it does not display the view that I showed in the above image, although it updates the table view. I want it to display an update management view every time it updates it. Can someone point out what I'm doing wrong? Thanks!

UPDATE: I added [self refreshControl beginRefreshing], and now the Spinner UIRefreshControl view is displayed, but it is higher than the first record in the table. Can someone point out how to fix it?

+4
source share
4 answers

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

I tried to figure out the differences between what refreshControl beginRefreshing itself and when I controlled the traction gesture. This is a pull operation.

And I checked the Apple 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. 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 mimic the traction gesture to trigger the refreshControl display.

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

He works!

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

+12
source

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

+1
source

There are two things you can do to add a UIRefreshControl in your table view, none of which are added to your code

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

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

If your class is a subclass of UITableViewController, try replacing

 self.refreshControl = refreshControl; with 2 line 
0
source

before code:

[refreshControl beginRefresh]

Enter a code:

[refreshControl layoutIfNeeded]

0
source

Source: https://habr.com/ru/post/1487052/


All Articles