uitableview + navigation controller set title from selected element

核能气质少年 提交于 2020-01-02 19:42:29

问题


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.



来源:https://stackoverflow.com/questions/9670797/uitableview-navigation-controller-set-title-from-selected-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!