【问题标题】:change navigation bar title's color更改导航栏标题颜色
【发布时间】:2010-01-11 08:27:19
【问题描述】:

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

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; 
} 

【问题讨论】:

  • 你的意思是你想让导航栏的标题在一个单元格被选中时改变?
  • 没有。当我想更改标题颜色并编写此代码时 label.text=@"Title";此代码更改了我选择的单元格标题.. 我只想更改颜色而不是标题
  • 如果你不想要自定义标题,那么不要写label.text=@"Title"; //CUSTOM TITLE ??我想我没有正确理解你......
  • OK 看假设,我们有表格视图和 5 个单元格,每个单元格都有一个特定的标题 Cell 1 / Cell 2 / Cell 3 等等......现在我想改变我的导航栏的颜色AnotherViewController 不是 RootViewController .. 所以当我使用该代码时,我的标题更改为 @"TITLE" 。例如,当我选择 CELL 2 / 我的 AnotherView 控制器的标题变为 CELL 2 时。如何在不更改单元格标题的情况下更改标题颜色?当我写这段代码时:label.text=@"Title";我 anotherviewController 的标题更改为 TILTE 。我不想根据单元格标签更改我的标题。只是颜色。

标签: ios objective-c uitableview


【解决方案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];

【讨论】:

  • 谢谢亚当,它可以工作:D,但如果我选择标题为 Cell 5 的任何单元格,仍然会有一点问题!为什么 ?如果我选择单元格 46,则标题为单元格 5 或任何数字!
  • 你在UITableViewDelegate中实现tableView:didSelectRowAtIndexPath:吗?它有什么作用?
【解决方案2】:

@亚当:

- (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!

【讨论】:

  • 好的,看来您正在将另一个控制器推送到导航控制器,并正在设置其导航项的标题。尝试使用label.text = self.navigationItem.title; 行进行试验,以便根据anotherViewController.navigationItem.title 设置标题,而不是self(我不知道self 在您问题的代码中是什么,我不知道是什么方法它在哪个班级)
  • 我试过用这一行 anotherViewController.navigationItem.title 但我得到了未声明的错误(首先使用这个函数)
  • Adam 请帮帮我,我的申请已经完成,我正在等待这个!