我试图在部分复制标题,如下图所示:

我试图在同一个视图中添加一个标签和分隔线。到目前为止,我的代码根本不起作用.
- (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,谢谢提前。
发布于 2013-12-13 11:10:45
下面的代码应该可以实现您想要的结果(包括底部的行):
- (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;
}发布于 2013-12-13 11:05:20
- (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元素提供适当的框架。在上面的代码中查找“//修复此”。
发布于 2013-12-13 11:05:31
尝试使用这种方法
-(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;
}https://stackoverflow.com/questions/20564924
复制相似问题