ios - 更改 UITableView 部分标题的颜色

标签 ios core-data

我有带有部分的 TableView,这些部分是国家/地区,我从我的核心数据实体中获取它们。我为我的表格 View 设置了背景图像,现在我想更改该部分的标题颜色。我怎样才能做到这一点?到目前为止,这是我的代码。

-(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];

}

当我尝试我的代码时,出现错误

最佳答案

您只需要在该方法中返回一个字符串(标题)。查看:

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

这就是您应该自定义外观的地方。

关于ios - 更改 UITableView 部分标题的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10312404/

相关文章:

ios - 立即调用 DispatchGroup notify() - 不等待进入/离开

ios - 无法使用 Google Map SDK 为 GMSPolyline 设置颜色

ios - 在 Xcode 项目中找不到 Firebase 文件

ios - 崩溃 : com. apple.main-thread EXC_BREAKPOINT 0x0000000100c5009c keyboard_arrow_up

swift - NSFetchedResultsController 触发的动画完成后执行代码

ios - TableController不显示来自Core-data的数据

ios - 在 coredata 加载时更新 Bool

ios - 将 PFFile 保存到核心数据中

ios - Objective-C 中的 Base 62 转换

ios - CoreData 的 XCTestCase 失败,返回 "could not locate the entity for myappTests.Example"