ios - didSelectRowAtIndexPath 方法与使用 Swift 2 的 Parse 中的 Load More 函数冲突

标签 ios swift uitableview didselectrowatindexpath pfquerytableviewcontrolle

我正在使用 Parse 创建我的应用程序。我的应用程序工作正常,直到我使用 didSelectRowAtIndexPath 方法,一旦我使用该方法将数据传递到另一个 View Controller 以显示详细信息,当我点击“加载更多”行时,我收到错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 13 beyond bounds [0 .. 12]'

我用谷歌搜索了一下,有人说: '问题可能出在您的 tableView:didSelectRowAtIndexPath: 方法中。当点击任何行时(包括 loadMore 单元格),将调用此函数。您可能想要检查正在点击的单元格是否是“加载更多”单元格,在这种情况下返回(并让对 super 的调用处理加载更多数据)。'

这对我来说相当合理,但这家伙没有给出解决方案,这是 2 年前的答案,而我现在正在使用 Swift。

这是我的代码,有人可以帮助我吗?谢谢。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var targetObject = objectAtIndexPath(indexPath)!

    gameIdSelected = targetObject["GameId"] as! String

    performSegueWithIdentifier("segue", sender: self)
}

最佳答案

您的答案仍然包含解决方案

This is called when any of your rows is tapped - including the loadMore cell

如果您的数据数组仅包含 13 个项目(从 0 到 12),如果您包含一个 loadmore 单元格,它将不起作用,因为您将有 14 个单元格,而不是 13 个。因此,如果您点击 loadmore 单元格,它会起作用。将尝试访问数组的第 14 个项目,但您只有 13 个项目,因此这就是出现此索引错误的原因。

如果您的单元格是 loadmore 单元格,请不要调用 objectAtIndex。像这样的事情:

if indexPath.row == 13 {
   //Do loadmore stuff
} else {
   var targetObject = objectAtIndexPath(indexPath)!

   gameIdSelected = targetObject["GameId"] as! String

   performSegueWithIdentifier("segue", sender: self)
}

当然不要硬编码 loadmore 值,但我不知道你的数据源名称。通常我们会做这样的事情:

if indexPath.row == yourDataSourceArray.count()

关于ios - didSelectRowAtIndexPath 方法与使用 Swift 2 的 Parse 中的 Load More 函数冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33184485/

相关文章:

ios - 在 xCode/Swift 中,修改字符串 "turn on Bluetooth to Allow [app] to Connect to Accessories"

ios - Swift iOS TableView 单元格项目在滚动时消失

iOS - 遍历单元格并检索数据

iOS - View Controller 响应来自 uiview 的数据更改

ios - Apple Mach -O Linker (Id) 错误?

ios - UIRefreshControl 出现在 Collection View 项的顶部而不是后面

ios - Swift 3 部分中的行数无效

ios - 使用委托(delegate)/协议(protocol)传递数据

ios - 获取动态表格 View 单元格高度和自动释放

ios - 更改 Twitter Fabric 应用程序的组织