0
votes

I am making an iPad SplitView App where I have the MasterView table on the left(in landscape orientation) and the DetailView on the right(nothing new so far...). When I click on a table cell, the detailView shows the cell details. I got also a button on the detialView to move forward to the next cell and a button to move backward to the previous cell.

My problem is that I would like to change the cell selection in the table view on the left every time I click on a button so the user knows on which cell he currently is.

I tried by calling the tableView: didSelectRowAtIndexPath: method but I can't seem to set the NSIndexPath right(I want to send an integer to indicate the number of the next cell).

Here is the code that's trzing to call the selectRowAtIndexPath: method:

iPadHelloWorldAppDelegate is the AppDelegate. masterViewController is the master controller with the table on it.

// method to set the image for the previous letter
-(IBAction)previousLetter{
    iPadHelloWorldAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSLog(@"IndexOfImage equals: %@", appDelegate.indexOfImage);

    int number = [[appDelegate.indexOfImage substringFromIndex:5] intValue];

    NSString *imageName = [NSString stringWithFormat:@"image%@.png",[NSString stringWithFormat:@"%d", number-1]];
    [self.letterImageView setImage:[UIImage imageNamed:imageName]];
    NSLog(@"Image name: %@", imageName);

    appDelegate.indexOfImage = [imageName substringToIndex:6];       

    NSIndexPath *currentPath = appDelegate.currentPath;
    NSIndexPath *nextPath = [NSIndexPath indexPathForRow:currentPath.row-1 inSection:currentPath.section];

    [masterViewController.tableView selectRowAtIndexPath:nextPath animated:YES scrollPosition:UITableViewScrollPositionNone];    

    NSLog(@"currentPath: %@", currentPath);    
    appDelegate.currentPath = nextPath;        
    NSLog(@"nextPath: %@", nextPath);    

Please help...

2
Here is what your problem is addressed. Should be exactly what you are looking for. - UPT
Hi, thanks for the answer but I tried that already and selectRowAtIndexPath will not even highlight the row like it did for the other guy. Which answer do you mean? The first with 'doSomeThing' method? didSelectRowAtIndex doesn't work for me neither... - iPhoneNoob

2 Answers

1
votes

I think this might work. For your Forward Button use this code

NSIndexPath *currentPath = [tableView indexPathForSelectedRow];
NSIndexPath *nextPath = [NSIndexPath indexPathForRow:currentPath.row+1 inSection:currentPath.section];
[tableView selectRowAtIndexPath:nextPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];

For the Back button, substitute -1 for +1.

See NSIndexPath UIKit Additions documentation for the indexPathForRow:inSection:method.

0
votes

the "tableView:didSelectRowAtIndexPath:" is a delegate method; you shouldn't be calling that directly. Instead, try calling "selectRowAtIndexPath:animated:scrollPosition:"