首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >区段中的标题

区段中的标题
EN

Stack Overflow用户
提问于 2013-12-13 11:02:43
回答 4查看 150关注 0票数 0

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

我试图在同一个视图中添加一个标签和分隔线。到目前为止,我的代码根本不起作用.

代码语言:javascript
复制
- (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,谢谢提前。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-12-13 11:10:45

下面的代码应该可以实现您想要的结果(包括底部的行):

代码语言:javascript
复制
- (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;
}
票数 1
EN

Stack Overflow用户

发布于 2013-12-13 11:05:20

代码语言:javascript
复制
- (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元素提供适当的框架。在上面的代码中查找“//修复此”。

票数 1
EN

Stack Overflow用户

发布于 2013-12-13 11:05:31

尝试使用这种方法

代码语言:javascript
复制
-(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;

}

//并编写此方法

代码语言:javascript
复制
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20564924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档