0

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

Amit
  • 4,837
  • 5
  • 31
  • 46
  • of course it's possible to update just progress bar frame in each tableview - that should be the proper solution. Reloading whole tableview just for that it's a disaster. But you should also show us the code you are using to update progression – Grzegorz Krukowski Aug 27 '17 at 11:13

2 Answers2

2

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() )
            }
        }
    }
Woof
  • 1,207
  • 1
  • 11
  • 21
0

you can use :

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

rather than using tableView.reloadData()

Please check below link:

It might help in your scenario.

Amit
  • 4,837
  • 5
  • 31
  • 46
  • I know this, however there is about 12 other views that get updated in each cell every time the cell is reloaded. I am trying to just update the progressViews in the cell and not waste cpu updating every other view in the cell. – Corey Smith Aug 27 '17 at 08:21