开发者

UITableView cell reuse causing NSTimer problems

开发者 https://www.devze.com 2023-01-25 20:21 出处:网络
I have a UITableView with a custom cell I built.The cell has several labels, a start button and a stop button, and an NS开发者_运维问答Timer that gets fired off and stopped from the buttons.

I have a UITableView with a custom cell I built. The cell has several labels, a start button and a stop button, and an NS开发者_运维问答Timer that gets fired off and stopped from the buttons.

The labels display data from sqllite (through core data) and the timer runs individually for each separate cell.

This all works fine until I have more cells and cell reuse gets triggered. At that point, each cell shows the correct data in its labels, but if I start a timer for the first cell, the moment it rolls off screen, the next cell showing (and subsequently reusing the previous cell) will have its timer going as well.

I can't figure out how to guarantee each cell has its own timer thread with cell reuse.

Any help would be appreciated.


In your case the cells have internal state (timer). When cell is reused, it's state is lost (or even worth reused cell will inherit previous state). You have two options

  • don't reuse cells
  • make cells stateless (e.g. move your timers to controller)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号