4

I want to implement Pull To Refresh to my UITableViewController by using UIRefreshControl. Here what i tried so far.

 - (void)viewDidLoad
    {
     .....
     ////********* Pull to refresh ************/
        UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
        refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
        refresh.tintColor = [UIColor magentaColor];
     //   [self.tableView addSubview:refresh];
// [self.refreshControl setEnabled:YES];
        [refresh addTarget:self action:@selector(pullToRefresh:) forControlEvents:UIControlEventValueChanged];
        self.refreshControl = refresh;

    }
    -(void)pullToRefresh:(UIRefreshControl *)refresh {
         [refresh beginRefreshing];
        refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing.."];
        [self loadRecentActivities];
    }

But when i pull the tableview neither activity indicator nor title is visible, however pullToRefresh is called and table refreshed.My app supports iOS7.0+. What i'm missing? Any help would be appreciated?

Edit: I'v tried with [self.refreshControl setEnabled:YES]; and [self.refreshControl setEnabled:YES]; as mentioned in my edited code, but still no luck. This is how my tableview looks like when i pull it to refresh-enter image description here

Final Solution: For table view background i was using

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
self.tableView.backgroundView = imageView;

Refresh indicator and title was hidden behind the self.tableView.backgroundView instead use

[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];

and it solves my problem. also many thanks to Balram Tiwari please see his answer if you still have problem.

Hiren
  • 12,720
  • 7
  • 52
  • 72
Bharat
  • 2,987
  • 2
  • 32
  • 48

2 Answers2

11

Just add self.tableView.backgroundView.layer.zPosition -= 1; under the initialisation code for refreshControl.

Slavcho
  • 2,792
  • 3
  • 30
  • 48
7

Actually you have your refresh controller there in the tableView, but it is behind the navigationBar. (Sounds different, right), Here is the catch,

When you are using code in iOS7, by default the UIViewController takes the (edgesForExtendedLayout) whole screen. That means your (0,0) will start from the top left corner, underneath the navigationBar.

So your tableView is actually starting from there. So to get rid of it, you just specify the edgesForExtendedLayout for your viewController in your viewDidLoad Method.

if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
    self.edgesForExtendedLayout = UIRectEdgeNone;

After that it should be visible.

  • Before above Fix:

Before Fix

  • After the Fix.

After the Fix

hope that helps.

Balram Tiwari
  • 5,657
  • 2
  • 23
  • 41
  • Also just uncomment this line. `[self.tableView addSubview:refresh];` – Balram Tiwari Feb 17 '14 at 08:54
  • Its not working for me..also i think its not due to edgesForExtendedLayout as you can see there is space(marked as red rectangle) when my tableview is being refreshed. – Bharat Feb 17 '14 at 09:19
  • FYI I'v added a subview on self.navigationController.view ( see-Call/Talk Activity in screenshot) and i tried by removing this, but refresh indicator doesn't appear in either case. – Bharat Feb 17 '14 at 09:24
  • So what was the issue Bharat ? – Balram Tiwari Feb 18 '14 at 04:49
  • edgesForExtendedLayout and constraints..anyway you helped me a lot thanks. – Bharat Feb 18 '14 at 04:55