Pull To Refresh in iOS 7

一曲冷凌霜 提交于 2019-12-18 11:35:20

问题


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?


回答1:


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.




回答2:


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



回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/19913473/pull-to-refresh-in-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!