uitableview + navigation controller set title from

2019-05-28 04:17发布

问题:

I have a UINnavigationController that handles the navigation in a UITableView.

When I select a row from the table I need to display in the UINavigationController title the selected item from the previous menu.

The label of the cell is read from an external xml that fills the rows of the UITableView.

How can I display my selection on the title of the UINavigationBar?

I've set a static title by using the self.title command, but it doesn't fit my needs

回答1:

Would this work for you?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    DetailTableViewController *detailController = [[DetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    //   Set the detail controller title to whatever you want here...
    detailController.title = @"Your title";

    //   Or set it to the title of this row
    UITableViewCell *cell = [[self tableView] cellForRowAtIndexPath:indexPath];
    detailController.title = cell.textLabel.text;

    [[self navigationController] pushViewController:detailController animated:YES];
}


回答2:

Normaly the UINavigationController always picks out the title of the UIViewController which has been actually loaded. Thats why you have to define it inside viewDidLoad:

self.title = @"Title";

The method displayed above seems to be more dynamic...but this also works.