Question

I've got a UITableView with several entrys, and these are divided up in several sections. If I click on one of the cells, a DetailViewController is accessed. In that DetailViewController, I want to use the data from the selected cell (the cell.textLabel and the cell.detailTextLabel), but I can't access these. Is there a possibility to solve the problem so I can access the textLabels from the selected cells with the DetailViewController?

Was it helpful?

Solution

One way is to add properties to DetailViewController for the two text values. Then in didSelectRowAtIndexPath, do something like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    //create your DetailViewController here
    UIViewController *detailViewController = ...

    //set the properties...        
    detailViewController.cellText = cell.textLabel.text;
    detailViewController.cellDetailText = cell.detailTextLabel.text;

    //push the detailViewController here

    [detailViewController release];
}

OTHER TIPS

Initialize the DetailViewController with the cell content.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    DetailViewController *detailViewController = [[[DetailViewController alloc] initWithText:cell.textLabel.text] autorelease];
    // push
}

Instead of the child reading from it's parent, It far cleaner to have the parent provide the info directly to it's child.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top