簡體   English   中英

如何僅針對 swift 中的唯一值更新 TableView 行?

[英]How do i update TableView row only for unique values in swift?

我有一個實時檢測對象的代碼,但是,這些對象幾乎每一幀都會重新加載,從而導致大量數據。 我想找到一種無需每秒重新加載幀即可檢測對象的方法。 我在下面附上了一些代碼片段,顯示了表格視圖是如何加載的。

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()
        }
    }

我沒有進入信號量討論,如果您想知道如何只更新表格視圖的 1 個單元格,您顯然需要知道哪個單元格,由它的 IndexPath 標識,然后通過該路由更新它

    let indexPath = IndexPath(row: 0, section: 0)
    tableView.reloadRows(at: [indexPath], with: .fade)

此代碼更新表格視圖第一部分中的第一個單元格,您需要設置

row: 0

section: 0

使用您要更新的表格視圖單元格的正確值。

如果您知道要重新加載哪個索引,可以使用以下 function:

tableView.reloadRows(at: [indexPath], with: .none)

要重新加載所有內容但保留偏移量,您可以使用此 function:

let contentOffset = tableView.contentOffset
tableView.reloadData()
tableView.setContentOffset(contentOffset, animated: false)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM