简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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