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

在此处输入图像描述

4

4 回答 4

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

要获得相同的 Button 图像,您可以使用自定义图像,通过使用默认图像,您不能那样显示。

于 2013-03-15T08:52:50.833 回答
1

您的代码中的问题是button框架是(275.0, 5.0, 30.0, 30.0),但myView框架是(0.0, 0.0, 300.0, 20.0). 的高度button是30,但myView只有20。的 Y 位置button为 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 。

这将解决您的问题。

于 2013-03-15T09:17:56.353 回答
1

对的,这是可能的。
为此,您必须使用 UITableView Delegate 方法创建自定义ViewUIButton返回:

- (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)];
于 2013-03-15T08:55:39.297 回答
0

设置标题视图的高度

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

    return 81;

}
于 2014-05-10T06:50:07.967 回答