Pregunta

I'm used sectioned tableView.If I click tableview always it passing indexpath 0 to detail view controller.If I click second row but it indexpath pass always pass 0.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    if ([segue.identifier isEqualToString:@"toNext"]) {
    detailTableViewController *detailVC = [segue destinationViewController];
    [detailVC setkMessageDict:(NSDictionary*)[nArray objectAtIndex:[self.mytableView indexPathForSelectedRow].section]];
}

What's wrong in code?

¿Fue útil?

Solución 3

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    if ([segue.identifier isEqualToString:@"toNext"]) {
    NSIndexPath *selectedIndex= [self.mytableView indexPathForSelectedRow];
    NSDictionary *myDic=(NSDictionary*)[nArray objectAtIndex:indexpath.section];
    detailTableViewController *detailVC = [segue destinationViewController];
    [detailVC setkMessageDict:(NSDictionary*)[[myDic objectForKey:@"locations"] objectAtIndex:selectedIndex.row]];
}

Please check your Array having the proper data from your section tableview otherwise add the NSDictionary value to array and pass it to detailTableViewController then try.Problem is your are not passing indexpath of section here.Hope its helpful for you.

Otros consejos

You need to create index-path object in to your seque like:-

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [self performSegueWithIdentifier:@"toNext" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    NSIndexPath *indexPath = [self.tableview indexPathForSelectedRow];
    NSLog(@" indexPath row %d",indexPath.row);
    NSLog(@" indexPath row %d",indexPath.section);
    detailTableViewController *detailVC = [segue destinationViewController];
    [detailVC setJobDetailDict:(NSDictionary*)[nArray objectAtIndex:indexPath.row];


}

Yes Ramdy, is right You need to mention row instead of section.
You use this....

[detailVC setJobDetailDict:(NSDictionary*)[nArray objectAtIndex:[self.mytableView indexPathForSelectedRow].row]];

Try updating the selected row indexPath using API:

selectRowAtIndexPath:animated:scrollPosition:

in the following UITableViewDelegate API:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * currentRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];  // Declare this row variable in .h file
    NSString * currentSection = [NSString stringWithFormat:@"%ld",(long)indexPath.section];  // Declare this section variable in .h file
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    if ([segue.identifier isEqualToString:@"toNext"]) {
    detailTableViewController *detailVC = [segue destinationViewController];    
    detailVC.PassRow = currentRow;
    detailVC.PassSection = currentSection;
}

The closest answer to the question is Nitin Gohel. But if you have connected the UITableViewCell to the detailTableViewController in the Storyboard, his method is also wrong.

In that case, the prepareForSegue method is provided with the correct UITableViewCell as a parameter stored in the sender parameter. This is entirely handled by UITableView. So the correct code would be:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([sender isKindOfClass:[UITableViewCell class]] && [segue.identifier isEqualToString:@"toNext"])
    {
        NSIndexPath *indexPath = [self.mytableView indexPathForCell:sender];

        detailTableViewController *detailVC = [segue destinationViewController];
        [detailVC setJobDetailDict:(NSDictionary*)[nArray objectAtIndex:indexPath.row];
    }
}

Do not forget to check for correct segue name.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top