objective-c - UITableView 以编程方式选择选项

标签 objective-c ios cocoa-touch uitableview

我有一个具有多个选项的 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/

相关文章:

ios - 以编程方式添加的 UIView 的自动布局

ios - 使用 sizeThatFits : doesn't work 调整 UIWebView 的大小

ios - iPhone SDK : UIWebView crash

ios - 将 TitleLabel 从 UIButton 发送到不同 UIView Controller 中的 UILabel,但未发送

iphone - 删除属性 PREBINDING 和 GCC_ENABLE_FIX_AND_CONTINUE

ios - prepareForReuse 之后的 UITableView - Cell 中的所有数据都消失了

ios - 许多教程介绍了在 Storyboard 中添加多个 subview Controller 的方法,为什么不直接添加多个容器 View 呢?

ios - 如何在代码中提取由 stringsdict 文件本地化的参数?

iphone - 在 Xcode Storyboard中使用可拉伸(stretch)图像

objective-c - 有没有办法从 UITableView 中删除分隔线?