尝试使用reloadsections重新加载时,节中的行数无效

时间:2017-10-21 06:12:05

标签: ios swift swift3

我在tableview中有两个部分。

  1. 第0部分的行在接收远程通知(插入)或过期的计时器(删除)时会发生变化。
  2. 第1节总是有8行,当用户刷新时会改变,所有8个项目都会改变。
  3. 我正在尝试使用更新日期集(使用刷新)后重新加载整个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部分使用了Realm Result,在第1部分使用了数组。

1 个答案:

答案 0 :(得分:0)

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