2012-10-01 25 views
13

Estoy intentando mover el UIRefreshControl sobre mi headerView o al menos hacer que funcione con contentInset. Alguien sabe cómo usarlo?UITableView y UIRefreshControl

Utilicé un headerView para tener un buen fondo al desplazarme por TableView. Yo quería tener un fondo desplazable.

- (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]; 
} 

Respuesta

32

no estoy realmente seguro de lo que su intención es con las cosas contentInset, pero en términos de la adición de un UIRefreshControl a un UITableView, se puede hacer. UIRefreshControl está pensado para ser utilizado con UITableViewController, pero si lo agregas como una subvista a una UITableView funciona mágicamente. Tenga en cuenta que esto es un comportamiento no documentado, y puede que no sea compatible con otra versión de iOS, pero es legal, ya que no utiliza API privadas.

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

crédito a @Keller for noticing this.

+3

Dentro de un UIViewController normal no tiene autoreconocimiento de control, por lo que está completamente equivocado lensovet – Godfather

+0

gran consejo ... thx – paiego

+1

Este método tiene algunos problemas si su tabla tiene una vista de título (en iOS7 al menos). Cuando se desplaza hacia abajo mientras se actualiza, las secciones de la tabla se salen todas de su lugar, compensadas por el alto de la vista del título. – AlBeebe

10

@ La respuesta de Echelon es perfecta, pero tengo una pequeña sugerencia. Agregue el control de actualización como @property para que pueda acceder a él más tarde.

en YourViewController.h

@property (nonatomic, strong) UIRefreshControl *refreshControl; 

en 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 
} 

Y la gran razón para hacerlo de esta manera ...

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

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

Sólo te da de clase acceso amplio a UIRefreshControl para que pueda finalizar Refrescando o verifique la propiedad isRefreshing de UIRefreshControl.

+1

'@property (no atómico, fuerte) UIRefreshView * refreshControl;' probablemente quiera decir 'UIRefreshControl' aquí. ¿Derecha? –

+0

@ self.name ¿Por qué llamaría 'taskDone' después de terminar? Me sorprende. de hecho, debería ejecutar 'taskDone' y volver a actualizarse, ¿estoy en lo cierto? +1 por su respuesta sin embargo. – Ron