Pull To Refresh in iOS 7

user717452 picture user717452 · Nov 11, 2013 · Viewed 21.7k times · Source

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?

Answer

Hardik Thakkar picture Hardik Thakkar · Dec 9, 2014

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