我正在尝试以编程方式选择表格 View 中的单元格。点击 selectRowAtIndexPath 时出现 SIGABORT 错误。 myIndex 的值为 1。我从已实现所需的 TableView 委托(delegate)和数据源方法的 viewController 中调用它。
override func viewDidAppear(animated: Bool) {
if myIndex != nil {
self.myTableView.selectRowAtIndexPath(NSIndexPath(index: myIndex), animated: false, scrollPosition: UITableViewScrollPosition.Middle)
}
}
最佳答案
注意控制台中的错误消息 - 它们在大多数情况下都非常有帮助。当您运行代码时,您将看到消息:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.
它描述了问题是什么以及如何修复它 - 您应该为 NSIndexPath 使用不同的初始值设定项
self.myTableView.selectRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0), animated: false, scrollPosition: .Middle)
关于在 tableview 上调用 selectRowAtIndexPath 时出现 Swift 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35708423/