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.
Related
I have progressBar with an ongoing progress-animation inside of a UICollectionViewCell. Every time I reload data of UICollectionView, the progress-animation gets removed.
I have to reload data every time the number of cells changes.
Use collectionView visibleCells property.
This way you have more control on the specific UI you want to UPDATE.
for example.
you have array of strings and dates.
the string is animation.
the date is simple label.
you want to update only the dates.
so :
1. you update the data (array) with the new dates.
2. you update the UI by calling collectionView visibleCells and then for each cell,
3. you update only the date with the new info. the animation keep
going.
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.
I have abit of a problem, I have a coredata object that is used to populate a UITableView. Each UITableViewCell has a couple of buttons that I am using as check boxes, when the user presses one of these text boxes I would like to update coredata and reload the UITableView so all of the arrays I have are updated to reflect the new data.
Thinking about this I have come to the conclusion that its abit redundant or overkill to be reloading the UITableView every time a button is pressed because some of these UITableViews will contain hundreds of rows with two editable UIButtons each.
So I thought that maybe I should update the current array instead then when the view is either exited or the device is put to sleep I could update the coredata object then? the only thing being I don't really know if this is the right thing to do or possible.
The reason this is such a problem is that when I change a button from say a tick to a cross if I scroll away then come back the buttons tick or cross s reverted to its old value.
I would like to know the best way to handle this case as I have never done anything like this before.
You should use an NSFetchedResultsController and its delegate methods to populate the UITableView. Then when the user taps a button, you simply update the corresponding Core Data entity and not the cell. The NSFetchedResultsController will then call its delegate methods, and you can update just that one cell on which the user made a change.
Also, in cellForRowAtIndexPath, you simple fid the corresponding CoreData entity, and use its attributes/properties to adjust the display of the cell.
Remember, that you must always use some data (NSArray usually) from which to read what to do for the cell at the indexPath when the tableView calls cellForRowAtIndexPath:.
This way, when the fetchedResutsController call its delegate methods, you can simply call reloadCellAtIndexPath on the tableView and then the tableView will call cellForRowAtIndexPath again. As the Core Data entity has been updated, your logic for adjusting the display for that cell will cause the cell to look as it should. It's important that you only ever adjust the way a cell looks in cellForRowAtIndexPath, and base the look on a CoreData entity. Change the look of a cell in multiple places, and you will get problems.
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.