mattnedrich
mattnedrich

Reputation: 8002

UIView drawRect only called once in UITableViewCell resulting in stale data when cells are re-used

I have a UITableViewCell that contains a custom UIView that I created (and some other stuff). My custom view extends UIView, and overrides drawRect for some custom drawing.

My tableview renders correctly when each cell is loaded initially, but when I scroll through the table cells are being re-used, and the custom UIView.drawRect method is not being re-called for them. This is resulting in a stale UIView being used for cells in the tableview.

Am I doing something wrong? I tried setting setNeedsDisplay on the tableview cell, but that did not work.

Upvotes: 1

Views: 720

Answers (2)

app4g
app4g

Reputation: 840

I was calling it within the cellForRowAt and it managed to update the charts on the custom Tableview UIView I put inside.

Code per below:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// this is cast AS! ErgWorkoutCell since this is a custom tableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "ergWorkoutCell") as? ErgWorkoutCell

cell?.ergWorkoutTitle.text = jsonErgWorkouts[indexPath.row].title
cell?.ergWorkoutTime.text = String(FormatDisplay.time(Int(jsonErgWorkouts[indexPath.row].durationMin * 60)))


cell?.ergLineChartView.setNeedsDisplay()


return cell!

  }

Upvotes: 0

mattnedrich
mattnedrich

Reputation: 8002

It turns out I was calling setNeedsDisplay on the UITableViewCell. When I call it on the custom UIView inside of the table view cell it works.

Upvotes: 2

Related Questions