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.
Related
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.
The problem I'm having is with triggering code as soon as a cell is displayed on screen.
What I have is grouped cells, much like in the settings application for iOS, with one segueing to a view with a date picker. After closing that view, i save the selected date, but I want to be able to fire my code so I can display the date in the detail text of a cell.
The cell that I want to display the date in has a swift file attached to it, as a UITableViewCell subclass/class.
I need a method similar to the viewDidLoad() on normal UIViewControllers where I can run code as soon as it appears on screen.
Something like this:
when cell comes on screen {
runCode()
}
I've tried with awakeFromNib, but with my understanding it only happens when the view is loaded from a previous view, not when it is transitioned back from a segue.
It looks like you're trying to refresh the contents of the cell, not looking for something similar to viewDidLoad().
What you'll want to do is after a user selects a date, you call the reloadData() on your tableview or to be more efficient, - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; with the updated cell's indexPath.
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.
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]).
Maybe I'm missing something obvious, but I'm have a hard time programmatically setting the row selection in a tableView. The goal is to simply have a tableView open with a row already selected. The problem appears to be that I have to wait until the tableView is fully loaded before I can modify the selection.
I've read various strategies such as calling reloadData for the tableView in the viewController's viewWillAppear method, then immediately calling selectRowAtIndexPath for the target row. But when I do that, I get a range exception because the tableView has zero rows at that point. The UITableViewDelegate methods (numberOfRowsInSection, etc.) don't appear to be called immediately in response to reloadData (which makes sense if the table rows are drawn "lazily").
The only way I've been able to get this to work is to call selectRowAtIndexPath after a short delay, but then you can see the tableView scroll the selected row into view.
Surely, there's a better way of doing this?
Well, you can use another strategy. You can create a hidden table view, configure how you want and than show to user. Use the tableview.hidden = YES.