如何解决如何仅在 swift 中为唯一值更新 TableView 行?
我有一个实时检测对象的代码,但是,这些对象几乎每帧都会重新加载,从而导致大量数据。我想找到一种无需每秒重新加载帧即可检测对象的方法。我在下面附上了一些代码片段,其中显示了表格视图是如何加载的。
Just for note the prediction is an array of type [VNRecognizedObjectObservation]
// var predictions: [VNRecognizedObjectObservation] = []
extension ViewController {
func predictUsingVision(pixelBuffer: CVPixelBuffer) {
guard let request = request else { fatalError() }
// vision framework configures the input size of image following our model's input configuration automatically
self.semaphore.wait()
let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer)
try? handler.perform([request])
}
// MARK: - Post-processing
func visionRequestDidComplete(request: VNRequest,error: Error?) {
self.measure.labell(with: "endInference")
if let predictions = request.results as? [VNRecognizedObjectObservation] {
// print(predictions.first?.labels.first?.identifier ?? "nil")
// print(predictions.first?.labels.first?.confidence ?? -1)
let pred = request.results?.first
// print(pred)
// print(predictions.first?.labels.first?.identifier as Any)
// print(predictions)
// This is where the table is being loaded each frame.
self.predictions = predictions
DispatchQueue.main.async {
self.boxesView.predictedObjects = predictions
self.labelsTableView.reloadData()
// end of measure
self.measure.end()
self.isInferencing = false
}
} else {
// end of measure
self.measure.end()
self.isInferencing = false
}
self.semaphore.signal()
}
}
解决方法
我没有进入信号量的讨论,如果你想知道如何只更新 tableview 的 1 个单元格,你需要知道哪个单元格显然是由它的 IndexPath 标识的,然后通过该路由更新它
let indexPath = IndexPath(row: 0,section: 0)
tableView.reloadRows(at: [indexPath],with: .fade)
此代码更新 tableview 第一部分中的第一个单元格,您需要设置
row: 0
和
section: 0
使用您要更新的 tableview 单元格的正确值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。