2017-09-17 5 views
0

UITableViewクラスのカテゴリを記述しました。そこで、リフレッシュコントローラを追加する方法を追加しました。Obj-CのaddTargetアクション関数からコールバックを追加

私のリフレッシュコントローラのターゲットメソッドは、main関数へのコールバックを提供します。私は私の中にリフレッシュコントローラへの呼び出しを与えている

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

#import <UIKit/UIKit.h> 

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

@interface UITableView (UITableView) 

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

@end 

の.h私のtableViewカテゴリ私のtableViewカテゴリー.M

ViewController名前:

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

     [self profileDetailsAPICall]; 
    }]; 
+1

カテゴリーは、保存されたプロパティを追加することはできません。 – Paulw11

+1

私はあなたが何を求めているのか、少し混乱しています。 'refreshTableView'の内部から' completionBlock'を呼び出せるようにするには、コールバックへの参照を格納する必要があります。カテゴリにはインスタンス変数を含めることができないため、カテゴリ内でこれを行うことはできません。 – RPK

+0

リフレッシュコントロールオブジェクト内にcompletionBlockを格納できますか? –

答えて

0

Iは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); 
} 
関連する問題