Iphone 添加按钮节标题?
我有一个带有两个部分的tableView,我想在部分标题中添加一个按钮(操作按钮),可以在示例图像中执行相同的操作吗Iphone 添加按钮节标题?,iphone,ios,objective-c,uitableview,Iphone,Ios,Objective C,Uitableview,我有一个带有两个部分的tableView,我想在部分标题中添加一个按钮(操作按钮),可以在示例图像中执行相同的操作吗 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if(section ==0) { return 0; } else{ UIView *myView = [[UIView alloc] initWithFrame:CGRectMa
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(section ==0)
{
return 0;
}
else{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[button setFrame:CGRectMake(275.0, 5.0, 30.0, 30.0)];
button.tag = section;
button.hidden = NO;
[button setBackgroundColor:[UIColor clearColor]];
[button addTarget:self action:@selector(insertParameter:) forControlEvents:UIControlEventTouchDown];
[myView addSubview:button];
return myView; }
}
像这样尝试可能会对你有所帮助
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//Headerview
UIView *myView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)] autorelease];
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[button setFrame:CGRectMake(275.0, 5.0, 30.0, 30.0)];
button.tag = section;
button.hidden = NO;
[button setBackgroundColor:[UIColor clearColor]];
[button addTarget:self action:@selector(insertParameter:) forControlEvents:UIControlEventTouchDown];
[myView addSubview:button];
return myView;
}
您可以在此处更改标题部分的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40.0;
}
要获得相同的按钮图像,您可以使用自定义图像,但使用默认图像无法显示。是的,这是可能的。
为此,您必须使用
UIButton
创建自定义视图,并从UITableView委托方法返回:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
///your implementation
return customView;
}
将视图高度设置为45,而不是20。太小了
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)];
代码中的问题是按钮
框架是(275.0,5.0,30.0,30.0
),但是myView
框架是(0.0,0.0,300.0,20.0)
。
按钮的高度为30,但myView
的高度仅为20。
按钮的Y位置为5,因此myView
高度需要为40
将myView
帧更改为(0.0,0.0,300.0,40.0)
使用heightForHeaderInSection
:委托方法将节的高度也设置为40
只需遵循以下步骤
将myView
帧设置为(0.0,0.0,300.0,40.0)
将按钮设置为(275.0、5.0、300.0、20.0)
使用heightForHeaderInSection
delegate方法将节的高度设置为40
这将解决您的问题。设置标题视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 81;
}
@Bhargavi UIView*View=[[UIView alloc]initWithFrame:CGRectMake(10.0,0.0,300.0,44.0)];//创建按钮对象UIButton*headerBtn=[[UIButton alloc]initWithFrame:CGRectZero];headerBtn.backgroundColor=[UIColor clearColor];headerBtn.不透明=否;headerBtn.frame=CGRectMake(10.0,0.0,100.0,30.0);[headerBtn setTitle:@“for状态:UIControlEventTouchUpInside];[headerBtn addTarget:self action:@selector(ActionEventForButton:)for controlEvents:UIControlEventTouchUpInside];[视图添加子视图:headerBtn];返回视图;如果你在viewForHeaderInSection中尝试了,那么问题是什么?@Bhargavi请检查我的代码它没有显示按钮吗?你调试过它是否在其他条件下运行吗?在第一个部分按钮没有出现,因为你没有保留剩余的部分按钮出现了什么问题?你到底想要什么?我想要按钮出现章节标题在样本图片中相同一旦看到该图片,你想这样吗?让我们再来聊一个疑问,请..简单的疑问而已