我有一个具有多个选项的 View ,其中是一个从服务器获取数据的 TableView 。
有没有办法让 TableView 在加载时自动选择第一个选项,即在 viewDidLoad
中?
最佳答案
尝试 UITableView's selectRowAtIndexPath:animated:scrollPosition:
方法。
- (void)viewDidLoad {
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
}
如果你想要委托(delegate)回调,你必须手动调用它:
if ([self.tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)])
[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
然后,您可能希望将该索引路径缓存在局部变量中。
关于objective-c - UITableView 以编程方式选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7166312/