Iphone 自定义TableView问题
我在iphone应用程序中有一个自定义标题视图。它上面有一个自定义按钮。 但那个自定义按钮动作并没有触发,而是触发了自定义类中的header tap事件。请帮助我。如何在header视图中对按钮触发动作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
- (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执行相同的操作。