自动单元格选择UITableView



我的UITableView通过PopOverViewController打开,所以我如何在app加载后自动加载这些单元格中的一个,

MainViewController上的单元格选择过程

- (void)setDetailItem:(id)newDetailItem {
    if (detailItem != newDetailItem) {
        [detailItem release];
        detailItem = [newDetailItem retain];
        //---update the view---
        label.text = [detailItem description];
    }
}

和TableViewController中的单元格选择:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    myAppDelegate *appDelegate = 
    [[UIApplication sharedApplication] delegate];
    appDelegate.viewController.detailItem = [list objectAtIndex:indexPath.row];  
}

我使用这个代码在TableViewController,但不工作!这意味着按下popOver按钮后,代码只是突出的单元格!!

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

我在不同的方法中使用上述代码,如viewDidAppear, viewWillAppeardidSelectRowAtIndexPath和…

Thank you

调用selectRowAtIndexPath:animated:scrollPosition:时,tableView:didSelectRowAtIndexPath:在委托上是而不是

From selectRowAtIndexPath:animated:scrollPosition: reference:

调用此方法不会导致委托接收tableView: willSelectRowAtIndexPath:或tableView:didSelectRowAtIndexPath: message,也不会发送UITableViewSelectionDidChangeNotification通知给观察者。

所以,不只是调用selectRowAtIndexPath:animated:scrollPosition::

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

你可以手动调用委托方法:

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

最新更新