Pull to refresh on UITableView Inside a UIViewController
Asked Answered
K

11

9

I am trying to implement the pull to refresh functionality in my application. The architecture is such that the there is a UITableView inside a UIViewController. I want to be able to refresh the tableview on pull down. I tried the code below in the viewDidLoad method, but it does not work. Can any one tell me where am I wrong in implementation?

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
    refresh.tintColor = [UIColor grayColor];
    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
    [refresh addTarget:self action:@selector(get_vrns) forControlEvents:UIControlEventValueChanged];
    [self.vrnTable addSubview:refresh];
Kirkcudbright answered 22/11, 2013 at 6:44 Comment(2)
possible duplicate of UIRefreshControl without UITableViewControllerOut
possible duplicate of Pull to refresh UITableView without UITableViewControllerElbring
N
-11

UIRefreshControl without UITableViewController

Or you can use UITableViewController instead of a UIViewController.

Newcomen answered 22/11, 2013 at 6:46 Comment(2)
Can't do that due to some limitations. Is there a work around such that I don't have to change the architecture.Kirkcudbright
Amar post good link above your question. #12498440Newcomen
P
15

Since you can't use a UITableViewController instead of UIViewController, try doing this :

UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = self.vrnTable;

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.tintColor = [UIColor grayColor];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[self.refresh addTarget:self action:@selector(get_vrns) forControlEvents:UIControlEventValueChanged];

tableViewController.refreshControl = self.refresh;

Hope this helps!

Panicstricken answered 22/11, 2013 at 7:3 Comment(0)
B
8
-(void)viewDidLoad
{
   UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
    //[self.mytable addSubview:refreshControl];
    UITableViewController *tableViewController = [[UITableViewController alloc] init];
    tableViewController.tableView = self.mytable;
    tableViewController.refreshControl = refreshControl;
}

-(void)refreshData
{
    //Put your logic here


   //reload table & remove refreshing image
   UITableViewController *tableViewController = [[UITableViewController alloc] init];
   tableViewController.tableView = self.mytable;
   [self.mytable reloadData];
   [tableViewController.refreshControl endRefreshing];
}
Bouie answered 27/6, 2014 at 6:26 Comment(1)
Could you add some explanation?Twentyfourmo
M
3

Updated answer for Swift 1.2

    var refreshControl = UIRefreshControl()
    refreshControl.backgroundColor = blue
    refreshControl.tintColor = UIColor.whiteColor()
    refreshControl.addTarget(self, action: Selector("yourFunctionHere"), forControlEvents: UIControlEvents.ValueChanged)
    self.tableView.addSubview(refreshControl)
Mintz answered 4/5, 2015 at 22:57 Comment(1)
adding refreshControll as tableview subview works also on objective cArnhem
R
2

you can see here : UIRefreshControl in UIViewController (with UITableView)

@interface MyViewController ()
{
    UIRefreshControl *refreshControl;
}
  @property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *refreshView = [[UIView alloc] initWithFrame:CGRectMake(0, 55, 0, 0)];
    [self.tableView insertSubview:refreshView atIndex:0]; //the tableView is a IBOutlet

    refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.tintColor = [UIColor redColor];
    [refreshControl addTarget:self action:@selector(reloadDatas) forControlEvents:UIControlEventValueChanged];
    /* NSMutableAttributedString *refreshString = [[NSMutableAttributedString alloc] initWithString:@"Pull To Refresh"];
    [refreshString addAttributes:@{NSForegroundColorAttributeName : [UIColor grayColor]} range:NSMakeRange(0, refreshString.length)];
    refreshControl.attributedTitle = refreshString; */
    [refreshView addSubview:refreshControl];
}

-(void)reloadDatas
{
   //update here...

   [refreshControl endRefreshing];
}

@end

http://www.g8production.com/post/79514553078/ios7-and-uirefreshcontrol-in-uiviewcontroller-with

Rheims answered 13/4, 2014 at 11:3 Comment(0)
M
2

If your app support iOS 6 (and later) only: I suggest UIRefreshControl

If also support iOS 5, you can use https://github.com/enormego/EGOTableViewPullRefresh

Mountbatten answered 27/6, 2014 at 8:31 Comment(0)
E
2

For Swift 3 and iOS backward compatibility

var refreshControl = UIRefreshControl()
let string = "Pull to refresh"
let attributedString = NSMutableAttributedString(string: string)
    attributedString.addAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 16)),NSForegroundColorAttributeName:UIColor.white], range: NSRange.init(location: 0, length: string.characters.count))
self.refreshControl.attributedTitle = attributedString
self.refreshControl.tintColor = UIColor.white
self.refreshControl.addTarget(self,
                             action: #selector(self.pulledDownForRefresh),
                             for: .valueChanged)
if #available(iOS 10.0, *) {
    self.accountSummaryTableView.refreshControl = refreshControl
} else {
    self.accountSummaryTableView.addSubview(refreshControl)
}

func pulledDownForRefresh() {
     //do some opertaion and then call
    self.refreshControl.endRefreshing()
}
Elmore answered 28/4, 2017 at 9:15 Comment(0)
C
0

I think you need to set the refresh control of the UITableView. I would need to see more of your code and view structure to diagnose the problem.

Here's a tutorial for objective-c and swift: http://www.jackrabbitmobile.com/design/ios-custom-pull-to-refresh-control/

Cavorelievo answered 27/2, 2015 at 15:10 Comment(0)
B
0

For Swift 3:

var refreshControl: UIRefreshControl!

override func viewDidLoad() {
    super.viewDidLoad()

    self.refreshControl = UIRefreshControl()

    self.refreshControl.tintColor = UIColor.black
    self.refreshControl.addTarget(self,
                                  action: #selector(ViewController.pullToRefreshHandler),
                                  for: .valueChanged)

    self.tableView.addSubview(self.refreshControl)
}

@objc func pullToRefreshHandler() {
    // refresh table view data here
}
Boccie answered 22/11, 2016 at 0:59 Comment(0)
N
-11

UIRefreshControl without UITableViewController

Or you can use UITableViewController instead of a UIViewController.

Newcomen answered 22/11, 2013 at 6:46 Comment(2)
Can't do that due to some limitations. Is there a work around such that I don't have to change the architecture.Kirkcudbright
Amar post good link above your question. #12498440Newcomen

© 2022 - 2024 — McMap. All rights reserved.