[英]Automatically cell selected 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
, viewWillAppear
和didSelectRowAtIndexPath
以及...
谢谢
当你调用selectRowAtIndexPath:animated:scrollPosition:
, 不会在委托上调用tableView:didSelectRowAtIndexPath:
selectRowAtIndexPath:animated:scrollPosition:
来自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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.