UITableViewCell selector setSelected:animated: gets called many times?

前端 未结 4 577
野趣味
野趣味 2021-01-31 19:56

I\'ve discovered a strange behavior with setSelected:animated: in my custom UITableViewCell class. I discovered that this function gets called multiple times if

4条回答
  •  太阳男子
    2021-01-31 20:29

    It should definitely be called when table is scrolled. Cells are reused, that means, if you scroll cells in invisible areas will be reused and reinitialized, including the call to setSelected, which is basically a lightweight property setter.

    If you really want to see what's happening, add a NSLog to tableView:cellForRowAtIndexPath: which will log indexPath and the returned cell.

    The entire log should give you a good understanding what happens inside and why. I suppose it will be something like this (Clicked on IndexPath 1:1)

    Give me cell on 1:0 (previously selected cell).
    Deselect 1:0
    Give me cell on 1:0 again (updated after deselection)
    Deselect 1:0 (update selected flag on this cell and trigger animation)
    Give me cell on 1:1
    Select 1:1
    Give me cell on 1:1 again (updated after selection)
    Select 1:1 (update selected flag on this cell and trigger animation)

    Clicking on a selected cell again is only slightly different - instead of triggering unselecting, it triggers another update.

提交回复
热议问题