2

My UITableView opens via PopOverViewController , so How can I load one of these cells automatically after app did load ,

the cell selecting process on MainViewController

- (void)setDetailItem:(id)newDetailItem {

    if (detailItem != newDetailItem) {
        [detailItem release];
        detailItem = [newDetailItem retain];

        //---update the view---
        label.text = [detailItem description];
    }

}

and cell selecting in TableViewController :

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

    myAppDelegate *appDelegate = 
    [[UIApplication sharedApplication] delegate];
    appDelegate.viewController.detailItem = [list objectAtIndex:indexPath.row];  

}

I use this code in TableViewController but does not work ! It means after press the the popOver button the code just highlight the cell !!

 [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];

I used above code in different methods like viewDidAppear , viewWillAppear and didSelectRowAtIndexPath and ...

Thank you

Code cracker
  • 3,105
  • 6
  • 37
  • 67
iOS.Lover
  • 5,923
  • 21
  • 90
  • 162
  • 1
    If you expect that calling `selectRowAtIndexPath:animated:scrollPosition:` will result in your `tableView:didSelectRowAtIndexPath:` getting called, that is just not going to happen as per the documentation: "Calling this method -`selectRowAtIndexPath:animated:scrollPosition:`- does not cause the delegate to receive a `tableView:willSelectRowAtIndexPath:` or `tableView:didSelectRowAtIndexPath:` message, nor will it send `UITableViewSelectionDidChangeNotification` notifications to observers." – albertamg Oct 05 '11 at 20:20
  • so is there any solution for that? – iOS.Lover Oct 05 '11 at 20:24
  • 1
    You could do [this](http://stackoverflow.com/questions/2035061/select-tableview-row-programmatically/2035171#2035171) or call `tableView:didSelectRowAtIndexPath:` yourself (on the delegate). – albertamg Oct 05 '11 at 20:30

1 Answers1

9

When you call selectRowAtIndexPath:animated:scrollPosition:, tableView:didSelectRowAtIndexPath: is not called on the delegate.

From the selectRowAtIndexPath:animated:scrollPosition: reference:

Calling this method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor will it send UITableViewSelectionDidChangeNotification notifications to observers.

So, instead of just calling selectRowAtIndexPath:animated:scrollPosition::

 [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];

you could call the delegate methods manually:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

if ([myTableView.delegate respondsToSelector:@selector(tableView:willSelectRowAtIndexPath:)]) {
    [myTableView.delegate tableView:self.tableView willSelectRowAtIndexPath:indexPath];
}

[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition: UITableViewScrollPositionNone];    

if ([myTableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
    [myTableView.delegate tableView:self.tableView didSelectRowAtIndexPath:indexPath];
}
albertamg
  • 28,492
  • 6
  • 64
  • 71
  • thank you this code works fine ! but after my app did load , the label.text does not change ,until user press the popOverView button – iOS.Lover Oct 05 '11 at 21:17
  • You update the label.text within `setDetailItem:`, which is called within `tableView:didSelectRowAtIndexPath:`, right? And now `tableView:didSelectRowAtIndexPath:` does get called. So, my advise would be to check if some variable is nil (`list`?, `label`?, etc.) by the time `tableView:didSelectRowAtIndexPath:` runs. – albertamg Oct 05 '11 at 21:33
  • There is no any var as nill , unfortunately this is not which I was expect , the user must press the popOver button to change the label , and another problem is when users select different cells If they press the button again the label string back as CELL 0 ! – iOS.Lover Oct 05 '11 at 21:52
  • Then I would suggest you to post a new question with the details of these other problems. – albertamg Oct 05 '11 at 22:30