2013-03-15 58 views
2

我有一个tableView有两个部分我想在节标题(动作按钮)中添加一个按钮可以在样本图像中做同样的事情吗?添加按钮部分标题?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

if(section ==0) 
{ 
    return 0; 
} 
else{ 
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
    [button setFrame:CGRectMake(275.0, 5.0, 30.0, 30.0)]; 
    button.tag = section; 
    button.hidden = NO; 
    [button setBackgroundColor:[UIColor clearColor]]; 
    [button addTarget:self action:@selector(insertParameter:) forControlEvents:UIControlEventTouchDown]; 
    [myView addSubview:button]; 
    return myView; } 
} 

enter image description here

+0

@Bhargavi的UIView *观的高度= [[UIView的页头] initWithFrame:方法CGRectMake(10.0,0.0,300.0,44.0)]; //创建按钮对象 UIButton * headerBtn = [[UIButton alloc] initWithFrame:CGRectZero]; headerBtn.backgroundColor = [UIColor clearColor]; headerBtn.opaque = NO; headerBtn.frame = CGRectMake(10.0,0.0,100.0,30。0); [headerBtn setTitle:@“”forState:UIControlEventTouchUpInside]; [headerBtn addTarget:self action:@selector(ActionEventForButton :) forControlEvents:UIControlEventTouchUpInside]; [查看addSubview:headerBtn]; return View; – 2013-03-15 08:52:28

+0

如果你在viewForHeaderInSection中试过,那么问题是什么? – 2013-03-15 08:56:43

+0

@Bhargavi请检查我的代码 – 2013-03-15 08:58:59

回答

7

试着像这可能是它可以帮助你,

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
    //Headerview 
    UIView *myView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)] autorelease]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
    [button setFrame:CGRectMake(275.0, 5.0, 30.0, 30.0)]; 
    button.tag = section; 
    button.hidden = NO; 
    [button setBackgroundColor:[UIColor clearColor]]; 
    [button addTarget:self action:@selector(insertParameter:) forControlEvents:UIControlEventTouchDown]; 
    [myView addSubview:button]; 
     return myView; 
} 

你可以改变这里的标题部分

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 
    return 40.0; 
} 

的高度,获得相同的按钮图像你可以通过使用你不能显示的默认图像来拍摄你的自定义图像吨。

+0

我已经试过了..没有同样的问题 – 2013-03-15 08:54:56

+0

你的问题是什么? – Balu 2013-03-15 08:56:13

+0

请检查我的代码 – 2013-03-15 08:59:49

1

是的,这是可能的。
的,你必须与UIButton创建自定义View从UITableView的委托方法返回:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
///your implementation 
    return customView; 
} 

让你的看法高度45,而不是20的太小。

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)]; 
+0

请检查我的代码 – 2013-03-15 09:00:48

1

在你的代码的问题是button(275.0, 5.0, 30.0, 30.0),但myView框架(0.0, 0.0, 300.0, 20.0)button的高度为30,但myView仅为20。 的button的Y位置是5,所以myView高度需要为40

变化myView帧(0.0,0.0,300.0,40.0)。

并使用heightForHeaderInSection:委托方法将该部分的高度设置为40。

只需按照此步骤....

  1. 设置myView(0.0, 0.0, 300.0, 40.0)
  2. 设置button帧为(275.0, 5.0, 300.0, 20.0)
  3. 使用heightForHeaderInSection委托方法将该部分的高度设置为40。

这将解决您的问题。

0

设置头视图

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 

    return 81; 

}