2012-03-20 58 views
0

我在我的iPhone應用程序中有自定義標題視圖。它有一個自定義按鈕。 但是這個自定義按鈕動作並不火,而是觸發了自定義類中的標題點擊事件。請幫助我。如何在標題視圖中的按鈕上觸發動作。自定義TableView問題

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

能否請您發佈您的代碼,這樣我們就可以幫你? – 2012-03-20 06:55:19

+0

嘿,只是取代我最近發佈的片段。它將解決您點擊的問題,即使您不需要使用自定義視圖或手勢。 – Kuldeep 2012-03-20 07:57:58

+1

您錯過了代碼中返回視圖的部分,或將該按鈕作爲子視圖添加到customView。我認爲這只是從問題而不是從你的代碼中丟失? – jrturton 2012-03-20 08:15:10

回答

0

您可以實現自己的回調方法來執行此操作。

我做的是我在customView中添加了TapGesture。當事件被觸發,我用一個回調方法來控制傳遞到我的tableView被Implemeted一個

你能做什麼在加載您可以將標籤值分配給customView

customHeader.tag = 1; 
頭中的viewController

,你可以在你的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

但問題是,我需要傳遞動態按鈕tag.how我會做UITapureureRecognizer同樣的事情。 – 2012-03-20 07:24:16

+0

好吧,我會更新我的答案,以滿足您的要求 – Krrish 2012-03-20 07:27:52

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