UIView drawRect只在UITableViewCell中调用一次,当单元格被重用时导致数据陈旧



我有一个UITableViewCell,包含我创建的自定义UIView(和其他一些东西)。我的自定义视图扩展了UIView,并覆盖了一些自定义绘图的drawRect。

我的tableview渲染正确时,每个单元格最初加载,但当我滚动通过表格单元格被重用,和自定义UIView。drawRect方法没有被重新调用。这会导致一个陈旧的UIView被用于tableview中的单元格。

我做错了什么吗?我尝试在tableview单元格上设置setNeedsDisplay,但这不起作用。

原来我在UITableViewCell上调用setNeedsDisplay。当我在表视图单元格内的自定义UIView上调用它时,它会起作用。

我在cellForRowAt中调用它它设法更新了我放在里面的自定义Tableview UIView上的图表。

代码如下:

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

最新更新