2017-08-27 54 views
0

我正在製作一個遊戲,其中有012 在UITableView。每個10 UITableViewCells有一個UIProgressView加上很多其他意見。我每1/10秒更新一次UITableView,這是非常緩慢的,滯後於舊設備。我每UX用1/10秒更新一次,給遊戲帶來平滑的進度感。多個UITableViewCells每個與UIProgressView更新非常緩慢

有沒有辦法只更新每個單元格中的進度視圖,而不必調用tableView.reloadData()來更新每個單元格中的所有視圖?

代碼示例:

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

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

      cell.progress.progress = Float(businessArray[indexPath.row-1].getCurrentProgress()) 

     //lots of other views are updated here 

     return cell 
    } 
} 

可能我也許改變這一行:

cell.progress.progress = Float(businessArray[indexPath.row-1].getCurrentProgress()) 

到這樣的事情:

cell.progress.progress = someVarLocalToViewControllerContainingTableView[indexPath.row] 

當我更新這個本地變量時,它只更新progressView或什麼? 我已經嘗試了許多方法,但無法弄清楚如何做到這一點...

+0

當然這可能只是更新每個tableview中的進度條框架 - 這應該是正確的解決方案。重新加載整個tableview只是因爲這是一場災難。 但你也應該向我們展示你用來更新進度的代碼 –

回答

1

如果你需要更新一個特定小區的進展,然後再調用這個

func reloadProgress(at index: Int) { 
    let indexPath = IndexPath(row: index, section: 0) 

     if let cell = tableView.cellForRow(at: indexPath) as? BusinessCell { 
      cell.progress.progress = Float(businessArray[index - 1].getCurrentProgress()) 
     } 
} 

如果你需要重裝所有酒吧表:

func reloadProgress() { 
     for indexPath in tableView.indexPathsForVisibleRows ?? [] { 

      if let cell = tableView.cellForRow(at: indexPath) as? BusinessCell { 
       cell.progress.progress = Float(businessArray[indexPath.row - 1].getCurrentProgress()) 
      } 
     } 
    } 
+0

這工作完美,從來沒有想過這樣做,謝謝! –

0

你可以使用:

self.tableView .reloadRows(at: <[IndexPath]>, with: <UITableViewRowAnimation>) 

,而不是使用tableView.reloadData()

請查看以下鏈接:

它可能有助於您的情況。

+0

我知道這一點,但是每次單元重新加載時,每個單元格中還有大約12個其他視圖被更新。我正在嘗試更新單元格中的progressViews,而不是浪費CPU更新單元格中的每個其他視圖。 –