我正在尝试复制标题部分,如下图所示:
我试图在同一视图中添加标签和分隔线。 到目前为止,我的代码根本不起作用......
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGRect sepFrame = CGRectMake(0, 50-1, 320, 1);
UIView *customView = [[UIView alloc] initWithFrame:sepFrame];
customView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:1.0];
UILabel *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull];
sectionHeader.backgroundColor = [UIColor whiteColor];
sectionHeader.textAlignment = NSTextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:16];
sectionHeader.textColor = [UIColor colorWithRed:(66/255.0) green:(139/255.0) blue:(18/255.0) alpha:1] ;
//sectionHeader.text = [NSString stringWithFormat:@"Section %d", section];
switch (section) {
case 0:
sectionHeader.text =NSLocalizedString(@"ACTIVO", nil) ;
[customView addSubview:sectionHeader];
break;
case 1:
sectionHeader.text = NSLocalizedString(@"PASSIVO",nil);
[customView addSubview:sectionHeader];
break;
}
return customView;
}
任何帮助都会非常感谢,因为我是IOS的新手,非常感谢。
答案 0 :(得分:1)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGRect sepFrame = CGRectMake(0, 50-1, 320, 44); //FIX THIS
UIView *customView = [[UIView alloc] initWithFrame:sepFrame];
customView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:1.0];
UILabel *sectionHeader = [[UILabel alloc] initWithFrame:sepFrame]; //FIX THIS
sectionHeader.backgroundColor = [UIColor whiteColor];
sectionHeader.textAlignment = NSTextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:16];
sectionHeader.textColor = [UIColor colorWithRed:(66/255.0) green:(139/255.0) blue:(18/255.0) alpha:1] ;
//sectionHeader.text = [NSString stringWithFormat:@"Section %d", section];
switch (section) {
case 0:
sectionHeader.text =NSLocalizedString(@"ACTIVO", nil) ;
break;
case 1:
sectionHeader.text = NSLocalizedString(@"PASSIVO",nil);
break;
}
[customView addSubview:sectionHeader];
return customView;
}
您需要为UI元素提供适当的帧。在上面的代码中查找“// FIX THIS”。
答案 1 :(得分:1)
尝试使用此方法
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CGRect sepFrame = CGRectMake(0, 50-1, 320, 50); // change here your view's height
UIView *customView = [[UIView alloc] initWithFrame:sepFrame];
customView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:1.0];
UILabel *sectionHeader = [[UILabel alloc] initWithFrame:sepFrame]; // also change here in label frame
sectionHeader.backgroundColor = [UIColor whiteColor];
sectionHeader.textAlignment = NSTextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:16];
sectionHeader.textColor = [UIColor colorWithRed:(66/255.0) green:(139/255.0) blue:(18/255.0) alpha:1] ;
//sectionHeader.text = [NSString stringWithFormat:@"Section %d", section];
switch (section) {
case 0:
sectionHeader.text =NSLocalizedString(@"ACTIVO", nil) ;
[customView addSubview:sectionHeader];
break;
case 1:
sectionHeader.text = NSLocalizedString(@"PASSIVO",nil);
[customView addSubview:sectionHeader];
break;
}
return customView;
}
//并编写此方法
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
答案 2 :(得分:1)
以下代码应该完成您想要的(包括底部的行):
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
customView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:1.0];
UILabel *sectionHeader = [[UILabel alloc] initWithFrame:customView.frame];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.textAlignment = NSTextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:16];
sectionHeader.textColor = [UIColor colorWithRed:(66/255.0) green:(139/255.0) blue:(18/255.0) alpha:1];
sectionHeader.text = NSLocalizedString(section == 0 ? @"ACTIVO" : @"PASSIVO", nil);
[customView addSubview:sectionHeader];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, customView.frame.size.height - 1, customView.frame.size.width, 1)];
lineView.backgroundColor = [UIColor colorWithRed:(66/255.0) green:(139/255.0) blue:(18/255.0) alpha:1];
[customView addSubview:lineView];
return customView;
}
答案 3 :(得分:0)
查看以下文档:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
它声明您还必须实施:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
让它正常工作。