Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull To Refresh in iOS 7

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?

like image 536
user717452 Avatar asked Nov 11 '13 18:11

user717452


4 Answers

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()
 }
like image 119
Hardik Thakkar Avatar answered Nov 15 '22 13:11

Hardik Thakkar


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.

like image 10
Infinity James Avatar answered Nov 15 '22 13:11

Infinity James


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

like image 2
Anthony Blatner Avatar answered Nov 15 '22 12:11

Anthony Blatner


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
like image 1
ipraba Avatar answered Nov 15 '22 12:11

ipraba