Question

Hi I am new to iOS programming and My requirement is to send the table cell text to detail view and display that text in detail view.

Master view

I have connected those view controller with segue. My master view controller is adding data in table view using below function.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [_types objectAtIndex:indexPath.row];

    return cell;
}

After that I am assigning the text of current selected table view cell to 1 variable using below function.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _type = [_types objectAtIndex:indexPath.row];
}

And for passing the selected table view cell's data to detail view controller I used below function.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"CellSelection"])
    {
        CellSelectionViewController *controller = (CellSelectionViewController *)segue.destinationViewController;
        controller.msg = _type;
    }
}

Detail View

In detail view I am just alerting the passed data sent from master view.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test" message:_msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

Now my problem is

  • When I click on any cell, it loads the detail view but sending null value.
  • When I go back and again click on a cell, it sends the value which I selected previously.

Anybody knows what is the problem here?

Was it helpful?

Solution

It looks like you're using didSelectRowAtIndexPath and a segue. You should use one or the other. So, you could either:

  1. You could retire your didSelectRowAtIndexPath method and then have a prepareForSegue that:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"CellSelection"])
        {
            CellSelectionViewController *controller = (CellSelectionViewController *)segue.destinationViewController;
            controller.msg = [_types objectAtIndex:[self.tableView indexPathForSelectedRow].row];
        }
    }
    
  2. Alternatively, you could remove the segue from the table view cell to the next scene, but rather define it to be between the two view controllers, give it an identifier, and then have didSelectRowAtIndexPath invoke it after your property was set:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        _type = [_types objectAtIndex:indexPath.row];
    
        [self performSegueWithIdentifier:@"yoursegueidhere" sender:@"self];
    }
    

But don't have both a segue from the cell and a didSelectRowAtIndexPath method. You have no assurances as to the order they will be performed. I'd be inclined to adopt the first approach and retire didSelectRowAtIndexPath altogether.

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