Swift 尝试为同一索引路径使多个单元格出队,这是不允许的

标签 swift tableview

我有一个包含各种不同单元格的 tableView,这些单元格的 textLabel 数字每 .1 秒增加一次。我没有每 .1 秒重新加载一次 tableView 或可见单元格,而是认为更改文本会更有效,但是当我尝试这样做时,应用程序因问题的标题。这是我的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! OwnedBusinessCell

    cell.textLabel?.text = ""

    return cell

}

调用我函数的计时器:

func scheduledTimerWithTimeInterval(){
    reloadTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.reloadTV), userInfo: nil, repeats: true)
}

我“尝试”更改文本的函数:

@objc func reloadTV() {

    for myIP in (tableView.indexPathsForVisibleRows)! {
        let cell = tableView(tableView, cellForRowAt: myIP)
        cell.textLabel?.text = increasingVariable
    }

}

完整的错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to dequeue multiple cells for the same index path, which is not allowed. If you really need to dequeue more cells than the table view is requesting, use the -dequeueReusableCellWithIdentifier: method (without an index path). Cell identifier: cell, index path: {length = 2, path = 0 - 0}'

感谢您的帮助!

最佳答案

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

是 TableView 数据源方法,不能直接调用, 这就是你在做什么

    let cell = tableView(tableView, cellForRowAt: myIP)

如果你想要一个特定的单元格,那么向 TableView 询问它,而不是 数据来源:

for myIP in tableView.indexPathsForVisibleRows ?? [] {
    if let cell = tableView.cellForRow(at: myIP) {
        // ...
    }
}

或者更简单:

for cell in tableView.visibleCells {
    // ...
}

关于Swift 尝试为同一索引路径使多个单元格出队,这是不允许的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49202588/

相关文章:

ios - 在 Swift 中以编程方式获取包标识符?

swift - 以编程方式在 header 中设置 2 个标签 - 未看到标签

ios - 松弛登录不起作用。 swift 3.0

objective-c - for 循环在editingStyleForRowAtIndexPath 中仅执行一次

ios - Tableview 以模态方式呈现和自动布局

objective-c - 停止 Xcode 自动为 Objective-C 头文件生成 Swift 接口(interface)

objective-c - Swift 调用包含 block 的 Objective-C 包装器函数

带按钮的 JavaFX-8 TableView 渲染了太多按钮?

javafx-2 - JavaFX 场景构建器 : how to display empty rows in table view? 如何在 tableview 中为 TableColumn 名称设置样式?

swift - 在 iOS 中加载 TableView 数据时出现 TableView 部分问题