iOS 7 中的下拉刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19913473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:51:29  来源:igfitidea点击:

Pull To Refresh in iOS 7

iosios7pull-to-refresh

提问by user717452

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

我正在尝试让下拉刷新功能在我的表视图中的 iOS 7 上正常工作。在 viewDidLoad 上,我有:

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:

当刷新方法调用的请求完成时,在 didCompleteRequest 代码中,我有:

[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?

在 iOS 6 上,这意味着当你下拉 table view 时,它会显示圆形箭头,当你拉的时候会被拉长,拉得足够远后,它会刷新。不过,现在我看不到圆形箭头,只有一个 UIActivityIndi​​cator。它也有时会起作用,有时不会。我错过了什么?

采纳答案by Infinity James

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

您正在谈论的“UIActivityIndi​​cator”是 UIRefreshControl 的新默认外观。

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

你下拉,当圆圈完成时,它显示你离触发刷新有多近。

回答by Hardik Thakkar

To add UIRefreshControl in your UITableView...

要在 UITableView 中添加 UIRefreshControl...

1) in ViewDidLoad..

1) 在 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...

2)调用相关方法刷新UITableView数据...

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

OR for Swift

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

回答by Anthony Blatner

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.Hymanrabbitmobile.com/design/ios-custom-pull-to-refresh-control/

这是我们的自定义下拉刷新控件教程(在objective-c 和swift 中):http: //www.Hymanrabbitmobile.com/design/ios-custom-pull-to-refresh-control/

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

希望有帮助,如果我能回答任何其他问题,请告诉我

回答by iPrabu

Update for swift

快速更新

For TableView

对于 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

对于 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.

在 UITableViewController 中有一个名为 refreshControl 的默认属性,默认为 nil。如果您只想初始化 refreshControl 并分配它。

let refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Please Wait..")
yourTableViewController.refreshControl = refreshControl