tags:

views:

274

answers:

2

hi i know how can change color of my navigation bar title but when i write this code :

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

i want my navigation bar shows the cell's title not custom tilte :) . here my table view codes :

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

    } 

sorry iam new to SDK

+1  A: 

Based on your explanation, try

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];
Adam Woś
thank you adam , it works :D but still has a little bit problem if i select any cell the title is Cell 5 ! why ? if i select cell 46 the title is Cell 5 or any number !
Momeks
Do you implement `tableView:didSelectRowAtIndexPath:` in `UITableViewDelegate`? What does it do?
Adam Woś
see the answer below :
Momeks
A: 

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

}

i creat a poem application so , i have 495 cell and 495 HTML file [poems] . each cell [POEM 1 to 495] has a special pooem . everything works fine but , this damn title color drive me crazy ... i put your code the color had changed but the title changed to Poem 5 !

Momeks
OK so it seems that you're pushing another controller to the navigation controller, and are setting the title of its navigation item. Try experimenting with the line `label.text = self.navigationItem.title;` so that you set the title based on the `anotherViewController.navigationItem.title`, not `self` (I didn't know what `self` was in the code in your question, I don't know what method it is in, in which class)
Adam Woś
i tried with this line anotherViewController.navigationItem.titlebut i got the error that undeclared (first use this function)
Momeks
Adam plz help me my application has been finished i am just waiting for this !
Momeks