ios - 尝试使用 reloadsections 重新加载时部分中的行数无效

标签 ios swift swift3

我在表格 View 中有两个部分。

  1. 第 0 部分中的行会在收到远程通知(插入)或计时器过期(删除)时更改。
  2. 第 1 部分始终有 8 行,并且会在用户刷新时更改,所有 8 个项目都会更改。

我正在尝试在日期集更改(使用刷新)后重新加载整个第 0 部分。

self.candidates.remoteAll()
for object in json {
    let candidate = Candidate(/* Some data */)
    self.candidates.append(candidate!)
}

self.tableView.reloadSections(IndexSet(integer: 1), with: .automatic)

它以某种方式抛出

Fatal Exception: NSInternalInconsistencyExceptionInvalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

不一致的行数不固定,有时更新前2条,更新后1条,有时更新前0条,更新后1条。我的猜测是当用户尝试刷新第 1 节时,第 0 节正在插入或删除行。

但我试图刷新第 1 节,第 0 节应该不会影响它是否具有一致的行数。我哪里做错了?谢谢。

更新

这是numberOfRowsInSection

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0:
        if let invitations = self.invitations {
            return invitations.count
        }
        return 0
    case 1:
        return self.candidates.count
    default: return 0
    }
}

我在第 0 部分使用领域结果,在第 1 部分使用数组。

最佳答案

我认为在这行代码中应该是:self.candidates.removeAll()

关于ios - 尝试使用 reloadsections 重新加载时部分中的行数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46860577/

相关文章:

ios - 将 json 数据保存在内部设备中

ios - 使用未声明的类型 'QLPreviewControllerDataSource'

ios - Swift 中的 NSLocalizedString 注释参数

android - 在 TWebBrowser 中将图钉添加到 Google map 并处理点击

ios - 如何从 iOS 中的字符串中删除第三个逗号 (,) 后的字符串?

ios - 如何在 imageView 中设置图像而不让 imagePickerController 隐藏它?

swift - 在解释响应之前,如何确保该函数完成?

iphone - 任何自动布局大师能够启发我有关约束和旋转的知识吗?

ios - 即使添加了所有约束,UITableViewCell 高度也不会动态计算

objective-c - 为 UIImageViwe 添加乘数时未获得 UIImageView 圆角边框