[英]adding a UIButton to tableView headerView
我試圖將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時,它在那里。 為什么是這樣?
您的addSource按鈕沒有任何框架。
使用此代碼...
-(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;
}
-(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;
}
請試試這個
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];
替換為
UIButton *addSource = [UIButton buttonWithType:UIButtonTypeCustom];
這將在標題視圖中顯示0,0點寬度100和高度45處的按鈕。 這會幫助你
設置ur按鈕的框架。按ur按鈕的默認框架將為CGRectZero手段。寬度n高度都將為零。.因此將其框架設置為在相對位置繪制。
- (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:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.