【问题标题】:Select uitableview row programmatically以编程方式选择uitableview行
【发布时间】:2012-11-28 07:50:43
【问题描述】:
我已经阅读了很多关于这个论点的内容,我已经测试了这个有效的例子:
source code example。但这不使用情节提要(我不知道这是否是使我的项目无法正常工作的原因)
我使用故事板制作了另一个项目,相同的指令顺序不起作用...
-(void)viewDidAppear:(BOOL)animated{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[firstController.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[firstTable.delegate tableView:firstTable didSelectRowAtIndexPath:indexPath];
}
这里是我的项目,和上一个链接一样,只是添加了viewDidAppear
my not working project
有人可以告诉我有什么问题吗?
谢谢
【问题讨论】:
标签:
objective-c
ipad
uitableview
【解决方案1】:
我试图研究你下载项目的代码。问题是 firstController.tableView 为 nil。
所以修复它:
[firstTable selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
或者将 firstController.tableView 分配给表格视图。
【解决方案2】:
- (void)viewDidLoad {
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
}
如果你想要来自委托的回调:
if ([self.tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}