在 tableview 上调用 selectRowAtIndexPath 时出现 Swift 错误

标签 swift uitableview tableview viewdidappear

我正在尝试以编程方式选择表格 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/

相关文章:

swift - 带有超链接文本的 UITextView

ios - 来自 RLMLinkingObjects 更改的 RLMResults 更改通知

swift - 动画多个原型(prototype) tableview 单元格

iphone - 在基于导航的应用程序中加载 UITableView 时出现问题

TableView 中的 iOS Swift 4 搜索栏

swift - 使用 iOS 共享/操作扩展截取主机应用程序的屏幕截图?

ios - ARKit 1.5 : Extract the recognized image

iphone - 是否可以向 UITableView 添加边框样式? (不是边框颜色/边框宽度)

iphone - TableView HeaderSection内存分配

ios - 添加到 super View 的 View 在导航 TableView 时消失