iOS.Lover
iOS.Lover

Reputation: 6051

Automatically cell selected UITableView

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

Upvotes: 2

Views: 7468

Answers (1)

albertamg
albertamg

Reputation: 28572

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];
}

Upvotes: 9

Related Questions