iOS infinite scroll items disapearing (sometimes) - ios

This is a very rare occurring bug from hell,
I have an infinite scroll controller that displays products, 2 in each row. Rarely, something affects the controller and causes items to vanish, when I tap the empty area where the item should be, it works as expected and directs the user to the item details controller. When I back out back to the list, sometimes the cell shows its content, and others get hidden.
Sometimes it just a couple of items missing, sometimes there are so many missing items that makes the list appear empty, like only 1 or 2 cells are visible per screen height.
An even stranger situation is, when I scroll really fast to the end and stretch the screen really fast out of the visible area, and there are no more items to load, the visible items can jump from left to right.
Please see these two videos.
https://www.dropbox.com/s/jibflcouz1ena8n/missingProductImages.mov?dl=0
https://www.dropbox.com/s/uz13fzorypnp38t/again.mov?dl=0
I could send code but I didn't want to clutter this place with full length code, let me know if you want to see a specific section of the code please. Maybe someone could have an idea of what might be going on by looking at the vids.

What's probably going on here is a state problem with your collection view cells. The code assigning model values to the cell's views would be of use here. But absent any actual information, the first thing to review would be the cell's prepareForReuse implementation:
Does it call super?
Does it clear out all current values?
Does it cancel any pending asynchronous operations?
Are fetches and cancellations correctly matched?
Next, check if there's any essential configuration in the cell's init/initWithCoder methods -- those are only called on first creation, not on reuse.
Those are the normal pitfalls of UICollectionView cell handling. If they don't suggest the problem, please post the cell code.

It looks like your cells are not being reused correctly.
Can you check that you have set the same reuseIdentifier for your cell in Interface Builder that you are assigning in your code?
Update: Attached image to show where to set the identifier in the storyboard/xib
Update: added layout solution
Another problem could be due to the layout bounds of your collectionViewCell. When you load your cells they bounds are not calculated until they have been added to your collectionView and rendered. This can cause the elements in your cell to layout with the wrong values. This happens commonly with async image frameworks as they cache a resized version of the image for performance. When you cell loads, the cached image is loaded at the wrong the wrong size.
Add the following code to your collectionViewCell:
- (void)setBounds:(CGRect)bounds {
[super setBounds:bounds];
self.contentView.frame = bounds;
[self setHighlighted:NO];
}

Related

ReactiveTableViewCell with dynamic height against cell reuse : how to avoid falling in an infinite loop?

So here's the description of the loop my app keeps falling on:
There is a UITableView containing ReactiveTableViewCell's (but I think RX part doesn't really matter here, so let's pretend these are UITableViewCell). Each cell embeds a UITextField upon an expandable/collapsable UIView (that allows me to show any error message related to the textfield) - when there is no error to show, this subview is collapsed.
The thing is, when UITableView gets scrolled and makes reuse of an "errored" cell, the subview of this cell is still expanded. As PrepareForReuse() method is made to reset cell state before a reuse, I thought it would be the right place to collapse the subview, and indeed it does... until the app falls into a loop, resizing top-screen and bottom-screen same cell forever.
How do you think I could fix this issue? Maybe is there a way to increase cell reuse tolerance so UITableView won't reuse a cell as soon as it disappeared from screen? Maybe shall I have a try with a resize-lock while list is being scrolled, but it will always leave some margin for error so it's not the fix I'm looking for.
Thanks for your help.
EDIT :
Well, it seems like the RX part was not so clueless. The IObservable I was subscribing to, aiming to show the error message, was a ReplaySubject. It appears that looping was happening on there due to this little guy, refreshing the UITableView (through TV.BeginUpdates/TV.EndUpdates) endlessly. So by now I assume ReplaySubject and cell-reuse won't work well altogether, since a simple replacement with a BehaviorSubject fixed the issue.

How to determine whether the user has scrolled past the first cell?

On iOS, when you append something to the beginning of UITableView's data source, the cell will "animate in" and push the rest of the cells down only if the user is already scrolled to the top. When the user is scrolled down farther down the list and a new item gets appended to the top, iOS does not automatically move the cells down.
When a new cell comes in from the top, how do I determine between the two?
My use case is that when the user has scrolled down past the iOS point, I can display a "new item above" button for the user to click . (similar to Twitter)
I'm using dynamic height for cells, so hard coding the scrollView by pixel isn't the most ideal way. I'd like to utilize iOS's way to determine it.
There is an array available of all visible cells' index paths called indexPathsForVisibleRows. You can access that property and check to see whether or not your first row is one of them, and go from there. You can also access visibleCells property to get the actual UITableViewCells themselves.
How to determine whether the user has scrolled past the first cell: a UITableView is a subclass of UIScrollView, so you can just look at the contentOffset property.
I'm not exactly sure that that's enough to accomplish what you want though. You could also maybe take advantage of the fact that tableView's cellForRowAtIndexPath: method only returns a cell if it's visible, and request the first cell. I've never tried implementing something like this before though so there might be a better approach.

UICollectionView stops loading cells after moveItemAtIndexPath:toIndexPath:

I have a UICollectionView with UICollectionViewFlowLayout which contains many items. I want to reorder a cell due to the user input.
[self.collectionView moveItemAtIndexPath:indexPath toIndexPath:newIndexPath];
After execution of this code, the collection view doesn't update it's contents when scrolling. It doesn't show next cells. It doesn't even call cellForItemAtIndexPath: but it's content size is correct (the layout "updates" it's state).
Therefore we can only see the cells which were distributed on the collection view before the reorder.
Any clues what might be the issue ? Are there any known workarounds for this issue? The app is iOS 8 only and the collection view is used with the UICollectionViewFlowLayout.
EDIT: When I call [self.collectionView reloadData]; the datasource is not even asked for anything, too.
The issue was with the UICollectionViewCell. High level description is that the cell is sent a method actionForKey from my code during the move animation. It caused the cell to stay in internal being animated state. I couldn't find reasonable workaround and refactored some bits in order to avoid actionForKey call. Now everything works as expected.

iOS Collection view cell update is delaying

I have sample collection view. I am dequeue cell from it by dequeueReusableCellWithReuseIdentifier:forIndexPath:
When ViewController is loaded the OS dequeue 5 cells and load information on it.
When I scroll, another 5-7 cells are dequeued and information is loaded on it.
The second set of loaded cells is coming in foreground with information from first set of loaded cells and the information in them is updated with fresh (right) one after less than a half second.
I am using prepareForReuse for cells, where I reset visibility of all cell components and than load fresh information, but the result is not what I expect, it is what I described above.
Any suggestions?
Thanks.
EDIT: Well if I update cell views with data from dequeueReusableCellWithReuseIdentifier:forIndexPath: all works fine, but if I pass the data to cell and ask cell to modify itself - than I have a problem pointed above.
EDIT 2: Soved. As I imagine - it was stupid mistake. As advice for others - make sure that you pass data to cell in any cases.
Do you have ALLOC statements in cellForItemAtIndexPath? or if using a subclass, do you have any ALLOC's in the subclass implementation? Possibly you seeing ui elements that are not being cleared out.

Resize TableCell on select/deselect

I'm new to iOS and MonoTouch, and I have a pretty basic need - I have two vastly different views to display in the same table cell depending on whether it is selected or not. When you load the app I'll present a list of products in a custom UITableView, with all the rows deselected and each row basically just showing the product names in a label. As soon as a user taps one of the rows and selects it, I show a very different view with more of a shopping cart layout. Since the two views are so different they have vastly different sizes. What i need is for the cell itself (or row?) to grow and shrink according to the natural height of whichever view is currently being displayed.
I'm using descendants of UITableViewController, UITableViewSource, and UITableViewCell for the solution. What I do is add both versions to the cell's ContentView, then set the Hidden property as needed when the cell/row is selected/deselected to show the right layout. That works well. I'm in demo mode right now, so I'm not overly worried about efficiency or responsiveness, that will have to come later.
I know that GetHeightForRow is responsible for sizing the rows but it only gets called once, when the cell is first shown. It seems to me that I need to alert the TableView to re-poll the source for a new size as the views are changing, but I can't figure out how to do it.
I tried (rather hopefully) to cause GetHeightForRow to be invoked again manually using Cell.SetNeedsLayout and Cell.SetNeedsDisplay, hoping that would cause the table to re-query the source for new dimensions, but no joy.
I tinkered with the direct approach, trying to size the contentview itself but it didn't seem as if it was leading anywhere. I feel as if the table needs to be told to query for a new row size, but I'm open to any suggestions.
Surely I'm not the first to attempt this? What am I missing?
Try forcing GetHeightForRow to be called again by calling ReloadRows instead.
You may or may not have to specify begin/end updates (this might just be for animation, though?)
tableView.BeginUpdates();
tableView.EndUpdates();

Resources