自动select单元格的UITableView

我的UITableView通过PopOverViewController打开,所以如何加载应用程序后自动加载这些单元格之一,

MainViewController上的单元格select过程

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

和TableViewController中的单元格select:

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

我在TableViewController中使用此代码,但不起作用! 这意味着按下popOverbutton后,代码将突出显示单元格!

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

我用上面的代码在不同的方法,如viewDidAppearviewWillAppeardidSelectRowAtIndexPath和…

谢谢

当您调用selectRowAtIndexPath:animated:scrollPosition:tableView:didSelectRowAtIndexPath: 不在委托上调用。

从selectRowAtIndexPath:animated:scrollPosition: reference:

调用此方法不会导致委托接收tableView:willSelectRowAtIndexPath:或tableView:didSelectRowAtIndexPath:消息,也不会将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]; }