Loading Timer in UITableviewcell - ios

I have a timer on multiple cell which updates every second. how can i implement NSTimer scheduledTimerWithTimeInterval in each cell. Is it possible to use single Timer which keeps track of the time of each cell. How can i implement this?.

Since the table view cells are made up of UI elements, I don't believe you can have dynamic timers as such. However, you could reload the table view every second, and you could set the timer to the appropriate value in each cell when reconfiguring the cells (which will happen after a call to [tableView reloadData]).

Related

The Amount of times a tableviewcell has been viewed and run a function based on it

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!
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.

Showing timer in my tableview cells

I'm showing Countdown timer in my Tableview cells for every product, having limited time offers.
As i scroll my tableview the values of timer are reflected to other cells. I know it is due to reuse of table view cells.
I want to show countdown timer for each product which are continuously changing for each product.
Any help will be appreciated.
Thanks in advance
You need to add observer in awakeFromNib() of your custom tableview cell class with a target function which update your UI of the cell. Then you need to declare a NSTimer variable inside your ViewController with target function which post the notification in regular intervals .You can set the initial values in cellForRowAtIndexPath method of the tableview.

How do I perform cell selection animations while using reloadData?

I have a UICollectionView, and I override setSelected in the UICollectionViewCell subclass to perform a little bounce animation when the user selects the cell.
The problem is that on selection of the cell, the very selection of the cell alters the data for the other cells (for instance, imagine the other cells have a label in each one that has the amount of cells selected displayed). So I call collectionView.reloadData() so that all the other cells update as well to show the new data (for instance, the amount of cells selected in a label).
However, this reloadData() call seems to reset the UICollectionView completely. Any animations taking place are stopped and the cells are simply updated without animation.
How do I have the cell selection animation, and update the other cells at the same time?
I've tried calling reloadData() only after the animation has completed, but this makes it look like there's lag/delay in updating the other cells (as it doesn't update them until a moment after when the animation finishes), which doesn't look very good.
Similarly, I've tried calling reloadItemsAtIndexPaths: (and exclude the selected cell that will animate), but for whatever reason with collection views this method seems really slow. Like there's a noticeable amount of lag after pressing it.
What should I be doing here? What's the standard for reloading data without the collection view destroying in-progress animations?
Reloading throws away the existing cells and creates (or reuses) new ones. So any animation is inherently destroyed. So, don't reload.
Instead, update your data model as usual and then get the currently visible index paths and associated cells and directly update those cells.

UITablieview with constantly changing values

Is it possible to use a UITableView in such a way that it's cells update their content indepedantly from the CellForRowAtIndexPath method?
Example: Showing cells with a timer which includes milliseconds.
Refreshing the table view every (say) 10 milliseconds in order to update the timer's display value for each cell will surely not work, well...
Tricky if you use re-usable cells. But if you know that you don't have too many rows, then you could sub-class UITableViewCell and do whatever you wanted to....
Just create a customCell and make it observe to the timer change using NSNotification. Now whenever the timer changes, simply post a notification with the value. Let the cells observe and update by themselves.

Highlight tableviewcell for a specific time

How can i highlight a uitableviewcell only for a specific period of time without selecting it? I need to keep a cell highlighted for suppose 3 seconds and it then get dehighlighted. Basically i am developing a book type application where cells are in synch with audio. i have populated the tableviewcells with text and also I have timings for how long to keep a cell highlighted.
The API for setting a UITableViewCell's highlight state is setHighlighted:animated: so assuming you just need a simple highlight the work is half done.
Using the UITableViewDelegate method tableView:willDisplayCell:forRowAtIndexPath:, notify the cell that it has become visible through some method you define in the subclass. That method will cause the cell to highlight itself and start a timer for as long as you want the cell to remain highlighted. When the timer fires, cause the cell to remove the highlight.
You will of course have to guard against timers remaining active if a cell scrolls out of view, so invalidate timers and reset the highlight state during prepareForReuse:
I think you should make custom UITableViewCell class. When cell will be initialized, use a NSTimer with a specific time interval. Make a custom method and pass it in below NSTimer method as selector (in custom cell class)--
[NSTimer timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats]
In selector method you can set the tableViewCell background colour to show it highlighted.

Resources