[英]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.