How to get a selected cell's title?

Discussion in 'iOS Programming' started by ataylor2009, Mar 18, 2010.

  1. ataylor2009 macrumors member

    Jan 27, 2009
    Okay, I know this question has an easy answer, but I'll be damned if I can figure it out...and I'm all out of clever search terms for the developer docs, but I know I'm close.

    I have a table view, and upon selecting a detail disclosure button, my user is presented with a new view. I'd like to set the new view's title to the selected cell's textLabel property like "ruleViewerController.title = ??"

    Can someone please put me out of my misery here? I know this isn't that hard, and I'll bet when I get the answer that it turns out to be on a page in the developer docs that I currently have open in another tab on my browser, but like I said - I'm currently stumped.

    Thanks in advance.
  2. skunkworker macrumors regular

    Sep 9, 2007
    Lets say you are tying an array to the table. If you click on a cell it should tell you which one was clicked and then you go to your data source and get that item.
  3. ataylor2009 thread starter macrumors member

    Jan 27, 2009
    Here's the code in question:

    -(void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath {
    RuleViewController *ruleViewController = [[RuleViewController alloc] initWithNibName:@"RuleViewController" bundle:nil];
    ruleViewController.title = @"Julie"; // this is what I need to make dynamic
    [self.navigationController pushViewController:ruleViewController animated:YES];
    [ruleViewController release];
    The commented line in the middle is the part I need help with; how do I get the selected cell's "textLabel" property, given only the indexPath? Like I said, I'm sure the solution is simple, but I'm still stumped.
  4. ataylor2009 thread starter macrumors member

    Jan 27, 2009
    Okay, I got it. Solution was

    UITableCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    NSString *cellTitle = cell.textLabel.text;
    ruleViewController.title = cellTitle;
    // push controller
    One step closer to more item crossed off the to-do list.
  5. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 3_1_3 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7E18 Safari/528.16)

    The accepted approach is to pull the info from the same data source that was used to populate the cell in the first place.

Share This Page