0

我正在尝试将 UIButton 添加到 tableView,但是当我执行以下操作时:

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frameWidth, 200)];

UIButton *addSource = [UIButton buttonWithType:UIButtonTypeCustom];
[addSource addTarget:self action:@selector(addBoard:) forControlEvents:UIControlEventTouchUpInside];
[addSource setImage:[UIImage imageNamed:@"addsource.png"] forState:UIControlStateNormal];
[addSource setBackgroundColor:[UIColor grayColor]];
[headerView addSubview:addSource];

self.tableView_.tableHeaderView = headerView;

我在那里没有看到 UIButton。当我尝试使用 UILabel 时,它就在那里。为什么是这样?

4

6 回答 6

1

您的 addSource 按钮没有任何框架。

于 2012-04-30T06:24:29.357 回答
1

使用此代码...

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

     UIButton *headername = [[UIButton alloc]initWithFrame:CGRectMake(20, 5, 270, 34)];
     headername.backgroundColor = [UIColor greenColor];

     UIView *headerView = [[UIView alloc] init];
     UIImageView *tempimage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 300,34)];
     tempimage.image = [UIImage imageNamed:@"GrayButton.png"];
     [headerView addSubview:tempimage];
     [headerView addSubview:headername];
     return  headerView;

}
于 2012-04-30T06:29:04.997 回答
1
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
     UIButton *name = [[UIButton alloc]initWithFrame:CGRectMake(30, 10, 120, 45)];
     name.backgroundColor = [UIColor greenColor];
     UIView *header = [[UIView alloc] init];
     UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 200,45)];
     image.image = [UIImage imageNamed:@"Button.png"];
     [header addSubview:image];
     [header addSubview:name];
     return  header;
}
于 2012-04-30T07:04:14.437 回答
0

请试试这个

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];

将其替换为

UIButton *addSource = [UIButton buttonWithType:UIButtonTypeCustom];

这将在标题视图中显示 0,0 点宽度 100 和高度 45 的按钮。这会帮助你

于 2012-04-30T06:31:13.147 回答
0

为您的按钮设置框架。通过您的按钮的默认框架将是 CGRectZero 意味着。宽度和高度都将为零..因此将其框架设置为在相对位置绘制。

于 2012-04-30T06:39:11.067 回答
0
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{        
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease]; 

    UIButton *addSource = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
    addSource.frame = CGRectMake(80.0, 0, 160.0, 40.0);
    [addSource setTitle:@"rep" forState:UIControlStateNormal];
    [addSource addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];        

    [headerView addSubview:addSource];
    return headerView;    
}

也不要忘记实施。

tableView:heightForHeaderInSection: 
于 2012-04-30T06:39:47.040 回答