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?

3
  • Just an activityindicator is the default appearance of UIRefreshControl in iOS 7. Also this is missing from your code: [self.myTableView addSubview:refreshControl];
    – Nikos M.
    Commented Nov 11, 2013 at 18:47
  • 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.
    – user717452
    Commented Nov 11, 2013 at 18:55
  • More detailed answer: stackoverflow.com/questions/22059510/… Commented Jun 11, 2014 at 3:44

4 Answers 4

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()
 }
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.

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).
    – user717452
    Commented Nov 11, 2013 at 19:20
  • 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. Commented Nov 11, 2013 at 19:23
  • 1
    Can you please tell me how can we remove he jerk or the small delay when we release it?
    – Itesh
    Commented Jan 21, 2014 at 7:29
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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.