在对this topic进行一些讨论后,我正在努力在UItableView上的footerview上实现2个按钮。要创建按钮,我在声明.h文件中声明它们,在实现.m文件上合成然后,我通过代码创建它们,如下所示:
- (UIButton *)resetButton{
if (resetButton == nil)
{
resetButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
resetButton.frame = CGRectMake(20.0, 40 , 95.0, 37.0);
[resetButton setTitle:@"Reset" forState:UIControlStateNormal];
resetButton.backgroundColor = [UIColor clearColor];
[resetButton addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchDown];
resetButton.tag = 1;
}
return resetButton;
}
然后,我将此代码实现到UITableView委托方法中:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 100.0)];
[customView addSubview:self.resetButton];
[customView addSubview:self.calculateButton];
return [customView autorelease];
}
通过这样做,按钮出现在屏幕上,但是当我点击它们时,没有任何反应(我在动作上实施了AlertView来检查它们是否有效。
这里有什么帮助吗?
谢谢!
编辑:链接到按钮的操作:
-(IBAction)resetAction:(id)sender {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Reset"
message:@"You just pressed the Reset button"
delegate:self
cancelButtonTitle:@"Acknowledged"
otherButtonTitles:nil];
[alert show];
[alert release];
}
答案 0 :(得分:5)
我认为你遗失的是- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
。
如果没有这个,自定义页脚将为高度0.您仍然可以看到该按钮的原因是默认情况下clipsToBounds
为NO
自定义视图。