Labels are lingering in my reusable table cells

后端 未结 3 838
半阙折子戏
半阙折子戏 2021-01-24 21:04

I have a table whose cells contain labels. Whenever I dequeue a reusable cell, the old labels are still lingering on it. I was able to remove them with this:

            


        
3条回答
  •  感情败类
    2021-01-24 21:25

    This kind of problem tends to happen when you add subviews to your cells in cellForRowAtIndexPath: regardless of whether it's being dequeued or newly created. As a result, you end up creating a new subview each time the row is reused, and the old subviews accumulate.

    What you instead want to do is to use the same subview each time, but just set the relevant attributes (e.g., labels or color) each time. Check out the answers to How do I clear a cell completely when I reuse it? to see some possible approaches.

提交回复
热议问题