简体   繁体   English

更改UITableView节标题的颜色

[英]change the color of UITableView section title

i have TableView with Sections, the sections are Countries and i fetch them from my core data entity. 我有带节的TableView,节是国家,我从我的核心数据实体中获取它们。 i set a background image for my tableview and now i want to change the title color of the section. 我为表格视图设置了背景图像,现在我想更改该部分的标题颜色。 how can i do that? 我怎样才能做到这一点? here is my code so far. 到目前为止,这是我的代码。

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];

    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(20, 6, 300, 30);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.shadowColor = [UIColor blackColor];
    label.shadowOffset = CGSizeMake(0.0, 1.0);
    label.font = [UIFont boldSystemFontOfSize:16];
    //label.text = sectionTitle;
    label.text=[sectionInfo name];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width,tableView.bounds.size.height)];

    [view addSubview:label];

    return [sectionInfo name];

}

when i try my code i get an error 当我尝试我的代码时出现错误

You just need to return a string (the title) in that method. 您只需要在该方法中返回一个字符串(标题)即可。 Check out: 查看:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

That is where you should be customizing the look. 那是您应该自定义外观的地方。

如果要更改UITableView中各节的颜色,则应使用UITableViewDelegate中的以下方法:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM