0

I want to fetch the amount of times a cell in a tableview has been viewed when it is completely in the view. That should only happen when it is completely in the view, minor parts of top and bottom cells shouldn't count.

And after a certain amount of time(~3 sec) has passed, it should trigger a function of print "hello".

I tried creating a timer and scheduling it in willDisplay method and invalidating it in didEndDisplayingCell method. It somehow takes into consideration the cells which are not fully in the view.

Also tried tableView.visibleCells and iterating cells in tableView.indexPathsForVisibleRows but nothing helped.

Any Help on this would be much appreciated. Cheers!

Aakash Dave
  • 866
  • 1
  • 15
  • 30

1 Answers1

1

While you want to keep track of how long a cell is displaying for (and the number of times it's been displayed), you don't actually want to keep these numbers in your custom (subclassed) UITableViewCell, because these cells get recycled and reused very quickly as they are scrolled on and offscreen.

Whatever your datasource object is, you should add a property (or two) to it to keep track of when the object is displayed in a cell (i.e. a "var displayCount : Int" property?), and you can start a Timer (or NSTimer) to count X seconds before displaying your "Hello" message.

You can detect whether a cell is fully visible via the methods found in the answers to this related question, which is when you can start up your Timer.

To detect when the cell is scrolled offscreen (so you can increment the display count and/or cancel the timer), use the delegate method didEndDisplayingCell.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Well, that was exactly what i had tried. But when scrolling through the row, it doesnt refresh the counter, though i have explicitly reset it. It just keep the previous value in the memory and allocates it to the total views. – Aakash Dave Aug 22 '17 at 22:28