如何在运行时预 select 一个 table 行?
How to pre-select a table row at runtime?
我想在加载视图时在 splitView 主控制器中预先 select 一个 table 行,就像 Apple 在设置应用程序中对常规 selection 所做的一样一个 Ipad.
我发现有些地方建议使用
let indexPathForSelection:NSIndexPath = tableView.selectRowAtIndexPath(indexPath: self, animated: false, scrollPosition: UITableViewScrollPositionNone)
但 Xcode 无法识别 scrollPosition 代码:UITableViewScrollPositonNone
作为有效标识符
这应该是一项简单的任务,但似乎没有人使用 Swift 来解决它。
你的代码有一些错误。
您的第一个错误是,您不能按照您的方式使用 selectRowAtIndexPath
方法。首先你必须设置NSIndexPath
。像那样:
var index = NSIndexPath(forRow: yourRow, inSection: yourSection)
此外,您不能像在 Objective-c 中那样在 Swift 中使用 UITableViewScrollPositionNone
。您必须使用:
UITableViewScrollPosition.None
但我建议您在您的情况下使用 Middle
:
UITableViewScrollPosition.Middle
所以为了得到你想要的结果,你应该做这样的事情:
var index = NSIndexPath(forRow: yourRow, inSection: yourSection)
tableView.selectRowAtIndexPath(index, animated: true, scrollPosition: UITableViewScrollPosition.Middle)
我想在加载视图时在 splitView 主控制器中预先 select 一个 table 行,就像 Apple 在设置应用程序中对常规 selection 所做的一样一个 Ipad.
我发现有些地方建议使用
let indexPathForSelection:NSIndexPath = tableView.selectRowAtIndexPath(indexPath: self, animated: false, scrollPosition: UITableViewScrollPositionNone)
但 Xcode 无法识别 scrollPosition 代码:UITableViewScrollPositonNone
作为有效标识符
这应该是一项简单的任务,但似乎没有人使用 Swift 来解决它。
你的代码有一些错误。
您的第一个错误是,您不能按照您的方式使用 selectRowAtIndexPath
方法。首先你必须设置NSIndexPath
。像那样:
var index = NSIndexPath(forRow: yourRow, inSection: yourSection)
此外,您不能像在 Objective-c 中那样在 Swift 中使用 UITableViewScrollPositionNone
。您必须使用:
UITableViewScrollPosition.None
但我建议您在您的情况下使用 Middle
:
UITableViewScrollPosition.Middle
所以为了得到你想要的结果,你应该做这样的事情:
var index = NSIndexPath(forRow: yourRow, inSection: yourSection)
tableView.selectRowAtIndexPath(index, animated: true, scrollPosition: UITableViewScrollPosition.Middle)