2017-08-27 58 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更新单元格中的每个其他视图。 –