2012-04-30 46 views
0

我想要一个UIButton添加到的tableView,但是当我做到以下几点:添加一个UIButton到的tableView headerView

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时,它就在那里。为什么是这样?

回答

1

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

+0

你是什么意思我ADDSOURCE没有任何框架? – xonegirlz

+0

你忘了设置一个框架。尝试'[addSource setFrame:CGRectMake(0,0,20,20)]' –

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; 

} 
+0

@xxonegirlz:请确实接受答案,如果这解决了你的问题不是为了我,而是为另一个(初学者和一些谁可能有同样的问题) 。所以他们可以选择解决方案,这对于堆栈溢出以及其他人来说是有用的。 – Nit

0

请试试这个

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

UIButton *addSource = [UIButton buttonWithType:UIButtonTypeCustom];

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

+0

这并没有帮助 – xonegirlz

+0

以及@ Nit的回答呢? –

0

为ur button.by设置框架ur按钮的defalult框架将是CGRectZero means.width n高度都将为零..因此设置其框架绘制在相对位置。

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: 
+0

这些框架只是为了举例。让它们符合你自己的要求 – ichanduu

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

通过向上箭头投票增加声望 – 2012-04-30 07:04:50

+1

你想告诉我们你的评论?如果你想动画人们投票你的答案,提供答案,回答这个问题。 OP询问“为什么”,你用一段代码回答(没有任何解释)。我永远不会赞成一个无法解释的纯代码答案(我自己的个人观点)。 – stema

+0

好吧不要生气..如果有需要解释,那么我准备好给予解释 – 2012-04-30 07:41:45

相关问题