2012-10-01 11 views
13

Sto provando a spostare UIRefreshControl sopra il mio headerView o almeno a farlo funzionare con contentInset. Qualcuno sa come usarlo?UITableView e UIRefreshControl

Ho usato un headerView per avere uno sfondo piacevole quando si scorre all'interno di TableView. Volevo avere uno sfondo scorrevole.

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

risposta

32

Io non sono davvero sicuro di quello che la vostra intenzione è con la roba contentInset, ma in termini di aggiunta di un UIRefreshControl ad un UITableView, si può fare. UIRefreshControl è in realtà inteso per l'uso con UITableViewController, ma se lo aggiungi come sottoview a UITableView funziona magicamente. Si noti che si tratta di un comportamento non documentato e potrebbe non essere supportato in un'altra versione iOS, ma è legale in quanto non utilizza API private.

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

Credito a @Keller for noticing this.

+3

All'interno di un normale UIViewController non hai self.refreshControl, quindi hai completamente sbagliato lensovet – Godfather

+0

ottimo consiglio ... thx – paiego

+1

Questo metodo ha alcuni problemi se il tuo tableview ha una vista titolo (almeno su iOS7). Quando si scorre verso il basso durante l'aggiornamento, le sezioni della tabella diventano tutte fuori posto, compensate dall'altezza della vista del titolo. – AlBeebe

10

@ La risposta di Echelon è azzeccata, ma ho un piccolo suggerimento. Aggiungi il controllo di aggiornamento come proprietà @ in modo che tu possa accedervi in ​​seguito.

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 
} 

E il grande motivo di farlo in questo modo ...

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

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

dà solo voi classe- ampio accesso a UIRefreshControl in modo da poter terminare il recupero o controllare la proprietà isRefreshing di UIRefreshControl.

+1

'@property (nonatomic, strong) UIRefreshView * refreshControl;' probabilmente si intende 'UIRefreshControl' qui. Destra? –

+0

@ self.name Perché chiamerà 'taskDone' dopo aver terminato? Mi sorprende. infatti, dovrebbe eseguire 'taskDone' e tornare di nuovo per aggiornare, ho ragione? +1 per la tua risposta però. – Ron

Problemi correlati