Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 自定义TableView问题_Iphone_Ios_Uitableview - Fatal编程技术网

Iphone 自定义TableView问题

Iphone 自定义TableView问题,iphone,ios,uitableview,Iphone,Ios,Uitableview,我在iphone应用程序中有一个自定义标题视图。它上面有一个自定义按钮。 但那个自定义按钮动作并没有触发,而是触发了自定义类中的header tap事件。请帮助我。如何在header视图中对按钮触发动作 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { NSNumber *num=[NSNumber numberWithInt:section]; U

我在iphone应用程序中有一个自定义标题视图。它上面有一个自定义按钮。 但那个自定义按钮动作并没有触发,而是触发了自定义类中的header tap事件。请帮助我。如何在header视图中对按钮触发动作

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

}

您可以实现自己的回调方法来完成此操作。

我所做的是在customView中添加了一个点击手势。触发事件时,我使用回调方法将控件传递给实现tableView的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
   }
}

只需粘贴此代码即可解决您的问题:

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

你能发布你的代码,这样我们就可以帮助你了吗?嘿,请替换我最近发布的代码片段。它将解决您的单击问题,即使您不需要使用自定义视图或手势。您在代码中错过了返回视图或将按钮作为子视图添加到自定义视图的部分。我想这只是问题而不是代码中缺少的?但问题是我需要传递动态按钮标记。我将如何使用UITapgestureRecognizer执行相同的操作。