14

I'm trying to move the UIRefreshControl on top of my headerView or at least getting it to work with the contentInset. Anyone know how to use it?

I used a headerView to have a nice background when scrolling within the TableView. I wanted to have a scrollable background.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Set up the edit and add buttons.

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundColor = [UIColor clearColor];

[self setWantsFullScreenLayout:YES];

self.tableView.contentInset = UIEdgeInsetsMake(-420, 0, -420, 0);

UIImageView *top = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top.jpg"]];
self.tableView.tableHeaderView = top;

UIImageView *bottom = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottom.jpg"]];
self.tableView.tableFooterView = bottom;

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"settingsIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(showSettings)];
self.navigationItem.leftBarButtonItem = leftButton;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addList)];
self.navigationItem.rightBarButtonItem = addButton;

//Refresh Controls
self.refreshControl = [[UIRefreshControl alloc] init];

[self.refreshControl addTarget:self action:@selector(refreshInvoked:forState:) forControlEvents:UIControlEventValueChanged];
}
gabac
  • 2,062
  • 6
  • 21
  • 30

2 Answers2

32

I'm not really sure what your intention is with the contentInset stuff, but in terms of adding a UIRefreshControl to a UITableView, it can be done. UIRefreshControl is actually intended for use with UITableViewController, but if you just add it as a subview to a UITableView it magically works. Please note this is undocumented behaviour, and may not be supported in another iOS release, but it is legal since it uses no private APIs.

- (void)viewDidLoad
{
    ...
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
    [self.myTableView addSubview:refreshControl];
}

- (void)handleRefresh:(id)sender
{
    // do your refresh here...
}

Credit to @Keller for noticing this.

Community
  • 1
  • 1
Echelon
  • 7,306
  • 1
  • 36
  • 34
  • 3
    Inside a normal UIViewController you dont have self.refreshControl, so you are completely wrong lensovet – Godfather Apr 04 '13 at 14:32
  • 1
    This method has some issues if your tableview has a title view (on iOS7 at least). When you scroll down while refreshing, the table sections get all out of place, offset by the height of the title view. – AlBeebe Apr 15 '14 at 04:30
10

@Echelon's answer is spot on, but I have one minor suggestion. Add the refresh control as a @property so you can access it later.

in YourViewController.h

@property (nonatomic, strong) UIRefreshControl *refreshControl;

in YourViewController.m

-(void) viewDidLoad {
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:self.refreshControl]; //assumes tableView is @property
}

And the big reason to do it this way...

-(void)refresh {
    [self doSomeTask]; //calls [taskDone] when finished
}

-(void)taskDone {
    [self.refreshControl endRefreshing];
}

Just gives you class-wide access to the UIRefreshControl so you can endRefreshing or check the isRefreshing property of UIRefreshControl.

Jawwad
  • 1,326
  • 2
  • 9
  • 18
self.name
  • 2,341
  • 2
  • 17
  • 18
  • 1
    `@property (nonatomic,strong) UIRefreshView *refreshControl;` you probably mean `UIRefreshControl` here. Right? – Emil Ahlbäck Jan 30 '15 at 09:16
  • @self.name Why would it call `taskDone` after finishing? It surprises me. in fact, it should execute `taskDone` and come back again to refresh, am I right? +1 for your answer though. – Ron Nov 02 '16 at 18:17