Search code examples
swiftxcodeperformanceuitableviewuiprogressview

Multiple UITableViewCells each with UIProgressView very slow to update


I am making a game which has 10 UITableViewCells in a UITableView. Each of the 10 UITableViewCells has one UIProgressView plus a lot of other views. I update the UITableView every 1/10th of a second, this is very slow and lags on older devices. I update it every 1/10th second for UX, gives a smooth progress view feel to game.

Is there a way to just update just the Progress Views in each cell individually rather than having to call the tableView.reloadData() that will update all the views in each cell?

Code example:

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

could I maybe change this line:

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

to something like this:

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

and when I update this local var it would update only the progressView or something? I have tried many ways but cannot figure out how to do this...


Solution

  • If you need to update a progress for a specific cell, then call this

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

    If you need to reload all bars in the table:

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