Pull To Refresh in iOS 7

六月ゝ 毕业季﹏ 提交于 2019-11-30 03:38:48

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.

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

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

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