我正在尝试将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时它就在那里。这是为什么?
答案 0 :(得分:1)
你的addSource按钮没有任何框架。
答案 1 :(得分: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;
}
答案 2 :(得分: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;
}
答案 3 :(得分:0)
请尝试这个
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];
替换为
UIButton *addSource = [UIButton buttonWithType:UIButtonTypeCustom];
这将在标题视图中显示0,0点宽度100和高度45的按钮。 这会对你有所帮助
答案 4 :(得分:0)
为你的按钮设置框架。按钮的defalult框架将是CGRectZero的意思。宽度n高度都是零。所以设置它的框架在相对位置绘制。
答案 5 :(得分: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: