2012-04-25 122 views
0

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

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

} 

当我尝试我的代码我得到一个错误

回答

1

你只需要在该方法返回一个字符串(标题)。退房:

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

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

+0

非常感谢你,它的工作原理!!!! – 2012-04-25 09:33:55

0

如果要更改您的UITableView部分的颜色,你应该使用这种方法从UITableViewDelegate

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