【问题标题】:reloadRowsAtIndexPaths crashes with JSON imageUrlsreloadRowsAtIndexPaths 使用 JSON imageUrls 崩溃
【发布时间】:2016-11-06 16:07:35
【问题描述】:

我有一个UITableView,我用来自 JSON 字符串的数据填充它。 看看pastebin中的代码。

那时:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.tableView.beginUpdates()
                self.tableView.reloadRowsAtIndexPaths(
                    [indexPath],
                    withRowAnimation: .Fade)
                self.tableView.endUpdates()
            })

它失败了。

我收到以下消息:

2016-11-06 16:32:06.579471 SO-33975355[4872:1389586] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 7 from section 0 which only contains 0 rows before the update'

如果我删除reloadRowsAtIndexPaths,它可以工作,但我不会更新行,除非您滚动或旋转设备。我当然希望它直接更新。

我想这与它的速度快有关吗?但是自从我修改了this from the example后,我完全一无所知。 (该示例不适用于 JSON,但使用来自维基百科的 url)

如果有人可以提供帮助,我会很高兴。

【问题讨论】:

    标签: ios json swift uitableview tableview


    【解决方案1】:

    在调用self.tableView.reloadRowsAtIndexPaths 之前,请确保您的tableView 已经创建并且您正确设置了dataSource。还应为您的 tableView 调用以下两个数据源(在 self.tableView.reloadRowsAtIndexPaths 之前)。

        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) {
            //Please return the maximum number of rows you are expecting from JSON. This is required because when you do reloadRowsAtIndexPaths, it should know that row index already exist
        }
            func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
           //Return cell
    }
    

    【讨论】:

      【解决方案2】:

      错误很明显:

      原因:'尝试从第 0 节中删除第 7 行,更新前仅包含 0 行'

      您正在尝试重新加载第 7 节中的第一行,但第 7 节的行数为零,因此没有第一行。

      三次检查您的数据模型。

      【讨论】:

        【解决方案3】:

        试试这个:

         self.tableView.beginUpdates()
         self.tableView.reloadRowsAtIndexPaths(
                            [indexPath],
                            withRowAnimation: .Fade)
         self.tableView.endUpdates()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-21
          • 1970-01-01
          • 1970-01-01
          • 2012-09-07
          • 1970-01-01
          • 2015-03-11
          相关资源
          最近更新 更多