我有一个包含各种不同单元格的 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/