2012-03-20 6 views
0

私のiPhoneアプリでカスタムヘッダービューがあります。それにはカスタムボタンがあります。 しかし、そのカスタムボタンアクションは火災ではなく、カスタムクラスにあるヘッダータップイベントが発生します。私に助けてください。ヘッダービューのボタンでアクションを発生させる方法。カスタムテーブルビューの問題

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    NSNumber *num=[NSNumber numberWithInt:section]; 
    UIView * customView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,50)]autorelease]; 
    customView.tag = section; 
    [customView setUserInteractionEnabled:YES]; 
    customView.backgroundColor = [UIColor blackColor]; 

    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame=CGRectMake(260, 7, 25, 25); 
    button.tag=section; 
    [button addTarget:self action:@selector(expandCollapsing:) forControlEvents:UIControlEventTouchUpInside]; 
    [button setImage:[UIImage imageNamed:@"add.png"] forState:UIControlEventTouchUpInside]; 
    [button setSelected:YES]; 

} 
+0

あなたは、あなたのコードを投稿してくださいことはできますか? –

+0

ちょっと、最近投稿したスニペットを置き換えてください。クリックの問題を解決します。カスタムビューやジェスチャーも使用する必要はありません。 – Kuldeep

+1

コード内でビューを返す部分が欠落しているか、カスタムビューにボタンをサブビューとして追加しました。私はこれがちょうどあなたのコードからではなく、質問に欠けていると思いますか? – jrturton

答えて

0

独自のコールバックメソッドを実装して、これを行うことができます。

私がしたことはです。私はTapGestureをcustomViewに追加しました。イベントは何ができるか私は私のtableViewが

をimplemetedされるのViewControllerに制御を渡すコールバックメソッドを使用解雇された場合には、ヘッダのロード中、あなたはのCustomView

customHeader.tag = 1; 
にタグ値を割り当てることができます

あなたのviewControllerでコールバックメソッドを実装することができます。 CustomheaderView

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkBoxEventHandler:)]; 
    [recognizer setNumberOfTapsRequired:1]; 
    [recognizer setNumberOfTouchesRequired:1]; 
    [self setGestureRecognizers:[NSArray arrayWithObject:recognizer]]; 


-(IBAction)checkBoxEventHandler:(id)sender 
{ 
    if ([self.delegate respondsToSelector:@selector(selectedHeader: atIndex:)]) 
    { 
      [self.delegate selectedHeader:self atIndex:self.tag]; 
    } 
} 

In your viewController.m 
-(void)selectedHeader:(myCustomHeader *)header atIndex:(int)index 
{ 
    //header -> will give you the same instance you created in your viewController while loading this custom header view 
    // So you can check the tag like this 
    if(header.tag == 1) 
    { 
     //Do Stuff here 
    } 

    // index -> we are passing headerViews tag as index ie header.tag is equal to index 
    // So checking the tag like this is the proper way 
    if(index == 1) 
    { 
     //Do Stuff here 
    } 
} 
+0

しかし、問題は、私はUITapgestureRecognizerで同じことをする動的なボタンtag.howを渡す必要があるということです。 –

+0

私はあなたの要件スイートに私の答えを更新します – Krrish

0

ただ、このコードはあなたの問題を解決します貼り付けます。私たちはあなたを助けることができるように

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame=CGRectMake(50.0, 0.0, 25, 25); 
    [button setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal]; 
    button.tag=section; 
    [button addTarget:self action:@selector(expandCollapsing:) forControlEvents:UIControlEventTouchUpInside]; 
    [button setSelected:YES]; 
    return button; 

} 
-(void)expandCollapsing:(id)sender { 
    UIButton *btn=(UIButton*)sender; 
    NSLog(@"Tag:%d",[btn tag]); 
} 
関連する問題