gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:36:50 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com