[英]Without reloading Tableview, update the changed index cell values using Observer in iOS Swift
In the first time I collect the array values from API response and displayed it in tableview, again I will get the same response from socket and reload the values in table, But here I don't need to reload entire table, I want update the cell's which value has been changed.我第一次从 API 响应中收集数组值并将其显示在 tableview 中,我再次从套接字获得相同的响应并重新加载表中的值,但在这里我不需要重新加载整个表,我想更新单元格的哪个值已更改。
Here Compare the two array's, from which index has changes, just need to update that index row cells only, without reload entire table view.这里比较两个数组,索引有变化,只需要更新索引行单元格,而不需要重新加载整个表视图。 old and new array , CodeSample
旧数组和新数组,代码示例
But you should be careful if will change some indexPaths in you data stack, if it gonna happen - use tableView.deleteRows
or tableView.deleteSections
.但是如果会更改数据堆栈中的某些 indexPaths,您应该小心,如果它会发生 - 使用
tableView.deleteRows
或tableView.deleteSections
。 This updates should be qual in table and in dataStack or crash
此更新在表和 dataStack 或
crash
中应该是合格的
let indexPaths = [IndexPath]()
tableView.performBatchUpdates {
self.tableView.reloadRows(at: indexPaths, with: .none)
}
or或者
tableView.beginUpdates()
tableView.reloadRows(at: indexPaths, with: .none)
tableView.endUpdates()
btw, you can make your indexPaths to update like let indexPaths = tableView.indexPathsForVisibleRows
- method's name is speechful, in case if you have socket I suppose it would be helpful since u've got dynamic顺便说一句,你可以让你的 indexPaths 像
let indexPaths = tableView.indexPathsForVisibleRows
一样更新 - 方法的名字很有趣,如果你有套接字我想它会有所帮助,因为你有动态
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.