2017-09-17 5 views
0

J'ai écrit une catégorie pour la classe UITableView. En cela, j'ai ajouté la méthode pour ajouter le contrôleur de rafraîchissement.Ajout de rappel depuis la fonction d'action addTarget dans Obj-C

Je souhaite que ma méthode de cible de rafraîchissement réponde à la fonction principale.

Mon tableView Catégorie .h

#import <UIKit/UIKit.h> 

typedef void (^UITableViewRefreshControllerCompletion) (UITableView *tableView); 

@interface UITableView (UITableView) 

-(void)addRefreshController:(UITableViewRefreshControllerCompletion)completionblock; 
-(void)removeRefreshController; 

@end 

Mon tableView Catégorie .m:

#import "UITableView+UITableView.h" 

@implementation UITableView (UITableView) 

-(void)addRefreshController:(UITableViewRefreshControllerCompletion)completionblock { 

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init]; 
    [refreshControl setTintColor:[UIColor appthemeblueColor]]; 
    [self setRefreshControl:refreshControl]; 

    [refreshControl addTarget:self action:@selector(refreshTableView:) forControlEvents:UIControlEventValueChanged]; 

} 

-(void) refreshTableView:(UIRefreshControl*)refreshControl { 

    completionblock(self); // I want this to call when this method is getting called 
} 



-(void)removeRefreshController { 

    if([self.refreshControl isRefreshing]) 
     [self.refreshControl endRefreshing]; 
} 

je donne dans mon appel au contrôleur de rafraîchissement ViewController as:

[self.profileDetailsrTableView addRefreshController:^(UITableView *tableView){ 

     [self profileDetailsAPICall]; 
    }]; 
+1

Catégories ne peuvent pas ajouter des propriétés stockées, donc il n'y a aucun moyen que vous pouvez stocker le bloc afin d'invoquer ultérieurement. – Paulw11

+1

Je suis un peu confus quant à ce que vous demandez. Si vous voulez pouvoir appeler 'completionBlock' depuis' refreshTableView', vous devez stocker une référence au rappel. Vous ne pouvez pas le faire dans une catégorie car les catégories ne peuvent pas contenir de variables d'instance. – RPK

+0

puis-je stocker completionBlock à l'intérieur de l'objet de contrôle d'actualisation? –

Répondre

0

Je résolus en prenant la sous-classe de UIRefreshControl et stocké le bloc d'achèvement;

SubClass-

#import <UIKit/UIKit.h> 

typedef void (^UITableViewRefreshControllerCompletion) (UITableView *tableView); 

@interface UIRefreshControlSubClass : UIRefreshControl 

@property(strong, nonatomic) UITableViewRefreshControllerCompletion completionBlock; 

@end 

Alors l'appel est devenu:

-(void)addRefreshController:(UITableViewRefreshControllerCompletion)completionblock { 

    UIRefreshControlSubClass *refreshControl = [[UIRefreshControlSubClass alloc]init]; 
    [refreshControl setTintColor:[UIColor appthemeblueColor]]; 
    [self setRefreshControl:refreshControl]; 
    refreshControl.completionBlock = completionblock; 

    [refreshControl addTarget:self action:@selector(refreshTableView:) forControlEvents:UIControlEventValueChanged]; 

} 

-(void) refreshTableView:(UIRefreshControlSubClass*)refreshControl { 

    refreshControl.completionBlock(self); 
}