更改导航栏标题颜色

时间:2010-01-11 08:27:19

标签: ios objective-c uitableview

我知道如何更改导航栏标题的颜色,但是当我编写此代码时:

UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,45,45)] autorelease];
label.textColor = [UIColor yellowColor];
label.backgroundColor=[UIColor clearColor]; 
self.navigationItem.titleView = label;
label.text=@"Title"; //CUSTOM TITLE
[label sizeToFit];

我希望我的导航栏显示单元格的标题,而不是自定义标题。

这是我的表格视图代码:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ghazalList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 
    if (cell == nil) { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease]; 
    } 

    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [ghazalList objectAtIndex:row];
    return cell; 
} 

2 个答案:

答案 0 :(得分:1)

根据您的解释,尝试

UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,45,45)] autorelease];
label.textColor = [UIColor yellowColor];
label.backgroundColor = [UIColor clearColor]; 
label.text = self.navigationItem.title;
self.navigationItem.titleView = label;
[label sizeToFit];

答案 1 :(得分:0)

@Adam:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!anotherViewController) {
    anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherViewController" bundle:nil];
    anotherViewController.navigationItem.title=[ghazalList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:anotherViewController animated:YES];
    [anotherViewController loadGhazal:indexPath.row];

}else {
    [anotherViewController loadGhazal:indexPath.row];
    anotherViewController.navigationItem.title=[ghazalList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:anotherViewController animated:YES];  
}

}

我创建了一个诗歌应用程序,所以我有495个单元格和495个HTML文件[诗歌]。每个单元[POEM 1到495]都有一个特殊的pooem。一切正常,但是,这个该死的标题颜色让我发疯...我把你的代码颜色改变但标题改为诗5!