0

Is there a way to apply a refresh control to a UITableView which is set in a UIViewController?

The solution I found requires subclassing of UITableViewController, which is troublesome for me because I need to subclass a UIViewController not a UITableViewController, and I don't see any method under the documentation of UITableView that allows me to set a refresh control on it.

Any help would be very much appreciated!

Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
JLT
  • 3,052
  • 9
  • 39
  • 86
  • Please check: http://stackoverflow.com/questions/10291537/pull-to-refresh-uitableview-without-uitableviewcontroller – Sandeep Agrawal Jul 03 '15 at 09:03
  • Possible duplicate of [Pull to refresh UITableView without UITableViewController](https://stackoverflow.com/questions/10291537/pull-to-refresh-uitableview-without-uitableviewcontroller) – RodolfoAntonici Jul 13 '18 at 14:27

2 Answers2

10

use this UIRefreshControl control :

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(refresh1:) forControlEvents:UIControlEventValueChanged];
[Delivered_TBL addSubview:refreshControl];


- (void)refresh1:(UIRefreshControl *)refreshControl
{
    [self Service_Call];
    [refreshControl endRefreshing];
}
Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
2
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.backgroundColor = [UIColor clearColor];
refreshControl.tintColor = [UIColor whiteColor];
[refreshControl addTarget:self
                   action:@selector(getLatestData)
         forControlEvents:UIControlEventValueChanged];

[yourTableviewName addSubview:refreshControl];


   -(void)getLatestData
{
 // here add your reload method
  [self XXXXX]; 

  NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  [formatter setDateFormat:@"MMM d, h:mm a"];
   NSString *title = [NSString stringWithFormat:@"Last update: %@", [formatter stringFromDate:[NSDate date]]];
   NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
                                                                    forKey:NSForegroundColorAttributeName];
   NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary];
        refreshControl.attributedTitle = attributedTitle;

        [refreshControl endRefreshing];
    }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143