2017-09-17 53 views
0

我已经为UITableView类写入了类别。在那里,我添加了添加刷新控制器的方法。在Obj-C中添加来自addTarget动作函数的回调

我想我的刷新控制器目标方法给主函数回调。

我的tableView类别.H

#import <UIKit/UIKit.h> 

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

@interface UITableView (UITableView) 

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

@end 

我的tableView分类.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]; 
} 

我在给调用刷新控制器我ViewController为:

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

     [self profileDetailsAPICall]; 
    }]; 
+1

分类不能添加存储特性,所以也没有办法,你可以在块存储以便稍后调用它。 – Paulw11

+1

我对你所问的有点困惑。如果您希望能够从'refreshTableView'内部调用'completionBlock',则需要存储对回调的引用。您不能在类别中执行此操作,因为类别不能包含实例变量。 – RPK

+0

我可以在refresh控制对象中存储completionBlock吗? –

回答

0

我解决了这个通过取UIRefreshControl的子类,并存储在完成块;

SubClass-

#import <UIKit/UIKit.h> 

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

@interface UIRefreshControlSubClass : UIRefreshControl 

@property(strong, nonatomic) UITableViewRefreshControllerCompletion completionBlock; 

@end 

所以通话变成了:

-(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); 
} 
相关问题