• Me
  • Tutorials
  • Blog
  • Little Bits
  • Poker

Luke Parham

iOS Developer + Other Stuff

  • Me
  • Tutorials
  • Blog
  • Little Bits
  • Poker

UITableViews are Liars

You know what's really useful information?

Knowing exactly when items in a scroll view come onscreen and then when they go offscreen. 

You would think you could get this information from a tableView's -tableView:willDisplayCell: method. And you kind of can, but not at first.

Turns out, when a table view is first loaded, this method will be called for _a bunch_ of cells. In my case it was like 7, even though only 3 were actually onscreen. Then -tableView:didEndDisplayingCell: was immediately called for those 7 cells.

giphy.gif

To get around this, I added a check to see if this call was happening when the table view's contentOffset y value was still at its original position. If it is, it's this initial round of setup that's done by the table.

When this is the case, you have to explicitly grab each cell and see if it's actually onscreen by using its frame.

If it's not, then just bail early. If it is, do any preloading you need to do.

Later, when the content offset isn't at its original position, that means you're scrolling the table. When this is the case, this method will accurately only be called right as the cell is coming onscreen.

Friday 05.04.18
Posted by Luke Parham
Newer / Older

Powered by Squarespace.