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.
Related
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 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.
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 tableview that is based on a array of DB results, these results contains a date field. I have a custom cell that contains an image and labels. I'm trying to do:
At cellForRowAtIndexPath I verify if the date of current item (objectAtIndex:indexPath.row) has date field bigger than the last item (objectAtIndex:indexPath.row-1). If this is true: I want to add a cell filling the ImageView with a certain image, but I need to add a new row just for show this image.
How can I do this? I'm already doing this verification, but I need to add a new cell...
Do not use the cellForRowAtIndexPath to decide how many cells you want to have. At the point this method is called you should have already setup the data source to provide table view with all information needed.
Here is what you need to do. Refactor your code in a way so you:
Setup the data source first.
Force reload of the table view either by calling the reloadData method.
hey you can add the object in your data base(for example ns array) and refresh the table view with method
[tableView reloadData];
then the method cell for row at index path will be called again and it will refresh the table view's items.just make sure the method cellforrawantindexpath in your code knows to handle the new data type(make validations).
Your tableView data source should not contain any of that logic where the content of once cell depends on the content of another cell. Instead, you should have a data item for each requested indexPath and that data item should contain ALL logic necessary for the cell to be configured. If an action on that cell has an effect on how another cell should look, you apply a change to the corresponding data-item, and then call reloadRowsAtIndexPaths: for the indexPaths.
In short: configure cells ONLY in tableView:cellForRowAtIndexPath: or tableView:willDisplayCellAtIndexPath, and ONLY do configuring. Other logic should be placed in some data(-controller) object.
As suggested, you should add an item to your data-array. Then call -insertRowAtIndexPath: on the tableView. ReloadData is like a big ugly hammer that you only use when ALL of the data for that tableView changes.
I am getting really frustrated trying to solve this problem i tried implementing it in many many ways but no solution. I have a UIStepper in a custom cell and i want to change a value on the cell. Everything works fine expect when i scroll around the tableView the values from the UIStepper changes from one cell to another. Please help here are my screen shoots.
Link to tableview implementation and link to cell implementation
You are trying to store the stepper value inside each individual cell. That's not going to work because cells are reused; the cell that you now see in row 2 may reappear in row 20 when the user scrolls.
That is why you must store the value for the stepper in the model (your data) on a row-by-row basis, so that you can set it freshly and correctly for that row every single time cellForRowAtIndexPath: is called.
This, in turn, means that as the user steps the stepper, its valueChanged is going to need to talk to the table view data source so that the model can be updated ("the stepper value for row 5 has just been changed to 3") and maintained in the model.