Pull To Refresh in iOS 7
Asked Answered
I

4

14

I'm trying to get the pull to refresh feature working properly on iOS 7 in my Table View. On viewDidLoad, I have:

self.refreshControl = [[UIRefreshControl alloc] init];

    [self.refreshControl addTarget:self action:@selector(refreshInvoked:forState:) forControlEvents:UIControlEventValueChanged];

I then run:

-(void) refreshInvoked:(id)sender forState:(UIControlState)state {
    // Refresh table here...
    [_allEntries removeAllObjects];
    [self.tableView reloadData];
    [self refresh];
}

When the request that the refresh method invokes is done, in the didCompleteRequest code, I have:

[self.refreshControl endRefreshing];

On iOS 6, this would mean that as you pull down on the table view, it would show the circular arrow that would get stretched out as you pull, and after pulled far enough, it would refresh. Right now, though, I see no circular arrow, just a UIActivityIndicator. It also sometimes will work and sometimes will not. What am I missing?

Insurgency answered 11/11, 2013 at 18:41 Comment(3)
Just an activityindicator is the default appearance of UIRefreshControl in iOS 7. Also this is missing from your code: [self.myTableView addSubview:refreshControl];Methodize
I don't see what difference that makes in the app. Tried with and without [self.tableView addSubview:self.refreshControl]; and didn't see anything change. @Nikos M.Insurgency
More detailed answer: #22060010Coombs
M
10

The 'UIActivityIndicator' that you are talking about is the new default appearance of a UIRefreshControl.

You pull down and as the circle completes it is showing how close to triggering a refresh you are.

Moulding answered 11/11, 2013 at 19:1 Comment(3)
Thanks, so nothing more is needed in terms of the code I posted? (I realize I left out the actual refresh method I created to populate the table).Insurgency
Nope, you're good to go. You can change the colour of it, but unfortunately there is no way to revert back to the old bogey-like style.Moulding
Can you please tell me how can we remove he jerk or the small delay when we release it?Degreeday
L
21

To add UIRefreshControl in your UITableView...

1) in ViewDidLoad..

- (void)viewDidLoad
{
    [super viewDidLoad];

    //to add the UIRefreshControl to UIView
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Please Wait..."]; //to give the attributedTitle
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    [tblVideoView addSubview:refreshControl];
}

2) call related method to refresh the UITableView data...

- (void)refresh:(UIRefreshControl *)refreshControl
{
    [self refreshTableData]; //call function you want
    [refreshControl endRefreshing];
}

OR for Swift

 let refreshControl : UIRefreshControl = UIRefreshControl.init()
 refreshControl.attributedTitle = NSAttributedString.init(string: "Please Wait...")
 refreshControl.addTarget(self, action: #selector(refresh), forControlEvents: UIControlEvents.ValueChanged)
 feedTable.addSubview(refreshControl)

 func refresh(refreshControl:UIRefreshControl){
      self.refreshTableData()//call function you want
      refreshControl.endRefreshing()
 }
Linoleum answered 9/12, 2014 at 5:17 Comment(0)
M
10

The 'UIActivityIndicator' that you are talking about is the new default appearance of a UIRefreshControl.

You pull down and as the circle completes it is showing how close to triggering a refresh you are.

Moulding answered 11/11, 2013 at 19:1 Comment(3)
Thanks, so nothing more is needed in terms of the code I posted? (I realize I left out the actual refresh method I created to populate the table).Insurgency
Nope, you're good to go. You can change the colour of it, but unfortunately there is no way to revert back to the old bogey-like style.Moulding
Can you please tell me how can we remove he jerk or the small delay when we release it?Degreeday
D
2

You can remove/hide the default activity indicator, and add in your own images and animations.

There's also a certain threshold value (distance) that the table must be pulled past before the refresh is invoked.

Here's our tutorial for Custom Pull to Refresh controls (in objective-c and swift): http://www.jackrabbitmobile.com/design/ios-custom-pull-to-refresh-control/

Hope it helps, let me know if I can answer anything else

Destiny answered 27/2, 2015 at 14:55 Comment(0)
L
1

Update for swift

For TableView

 - (void)viewDidLoad
 {
    let refreshControl = UIRefreshControl()
    refreshControl.attributedTitle = NSAttributedString(string: "Please  Wait..")
    tableView.addSubview(refreshControl)
    refreshControl.addTarget(self, action: #selector(refreshTable), forControlEvents: UIControlEventValueChanged)
 }

- (void)refreshTable {
    //Refresh Data here
    //......
    //Once all the data is fetched. If you are loading asynchronously add the below code inside the async block
    refreshControl.endRefreshing()
    [tableView reloadData];
 }

For UITableViewController

In UITableViewController there is a default property called refreshControl which by default is nil. If you want just initialise the refreshControl and assign it.

let refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Please Wait..")
yourTableViewController.refreshControl = refreshControl
Limn answered 22/4, 2016 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.