Retaining UICollectionViewCell while scrolling a UICollectionView - objective-c

I'm trying to figure out a way to keep a UICollectionViewCell from being removed (didEndDisplayingCell) from my UICollectionView when its original scroll position scrolls off of the view. I know this is the way a UICollectionView is supposed to work, that cells are removed and returned to the reuse pool when they are no longer visible, but this is based on their original position in the UICollectionView. I'm trying to drag that cell past the original visible area into a further away location, but it gets removed killing the pan gesture in progress and killing its view in the collection view once its original position is scrolled out of view. Is there a way to override this behavior and hold onto a UICollectionViewCell until I'm done with it?
I've seen the other ways to attack this problem by creating an image of the cell and dragging that instead, but that won't satisfy my purposes. I need the original cell to survive beyond its original screen position as I scroll through the UICollectionView.

Make copy when dragging your cell around and hide original cell.

Related

Select CollectionViewCell when scrolled into view

I have a collection view that is 100 points wide with a number of cells that are each also 100 points wide. I've got it setup to support scrolling and paging horizontally so the user can flick left and right and each cell occupies the entire collection view frame.
Originally, I had no scrolling and the user would tap each cell to activate something in the app. Now that its a narrow frame where only one cell can be seen at a time I feel that tapping is redundant and the cell should simply be tapped in effect when it comes in view.
Is there a way to trigger an event such as didSelectItemAtIndexPath when a given UICollectionViewCell becomes the one displayed within my collection view's frame?
UICollectionView responds to selectItemAtIndexPath:animated:scrollPosition:.
It also responds to indexPathsForVisibleItems which will be useful to determine where to make the selection.
You also need to decide when to make the selection, probably best after receiving scrollViewDidEndDecelerating: which the delegate inherits from UIScrollViewDelegate).
But it may be even better advice to look at what your code does upon selection and just do that (launched from the same place in code, probably when scrolling is finished), leaving selection out of it.

UICollectionView does not reveal all cells in Simulator

I am not very experienced with UICollectionView and Auto-Layout.
I have a very basic UICollectionView inside a root view. The root view seems to have a Navigation Item.
I created a custom cell which is really a square in proper size to allow 2 such cells per row using the FlowLayout.
There are 8 such cells in the view - it is basically a grid. I have absolutely no constraints on the view.
The collection view occupies the entire size of the root view according to the IB and in the editor, the first cell is placed right under the navigation item which is perfect.
However, when I ran the app in the simulator and scrolled the collectionview all the way to the button, I see only a partial fragment of both cells in the last row. I can see I have scrolled all the way down...
Obviously there seems to be some sort of overflow...but I would have thought uicollectionview would automatically handle the overflow and make all the cells visible?
When I set number of items in IB I can see that the cells in the last row are only partially visible.
Seems like content size of the collection view is not right - that's my guess..
What do I need to do to adjust things in IB so that all of my cells are visible...I do not want to hardcode any values in the code...
Thanks.
Solved. It turns out simulator does not show the entire collection view...the same shows fine on the device... What a headache...

Animating cells while UITableView is scrolling

I'm trying to achieve an effect wherein as a tableview is scrolling, the currently visible cells will animate according to where they are positioned on the screen. I'm somewhat new to IOS dev, so let me try to break it down:
Tableview loads with custom cells User begins scrolling While the
table is in motion, the visible cells have a UIView in which I would
like to perform an animation that corresponds directly to the cell's
current Y position on the screen.
Cells will ONLY animate when they
are visible
Cell animation directly corresponds to table motion, i.e.
whenever the table view scrolls, the cells are animating; once the
table stops, the animations pause
One solution I have thought of is to update drawRect every single frame when the tableview is moving. Depending on the graphic operations, this could be horrible performance wise. Is there a way to grab the current table position every time it changes? Would I be better suited to use Core Animation?
This might be helpful if you want to achieve something like below:
https://github.com/mrugrajsinh/AnimatedTableViewCellDemo
Make your view controllers the delegate of the UITableView, and use all the methods of scrollview Delegate also. There you have many controls for start scrolling and end scrolling, and within thpse controls you could check for uitableview cell positioning.
You have to become delegate to this: https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html
and this is the method to override and animate your cells:
– tableView:willDisplayCell:forRowAtIndexPath:

Is there quick way to know what cell is out of device screen in UITableView?

I have tableView with a lot of cells. I need to remove cells, which user passes while scrolling the tableView.
cellForRowAtIndexPath is called when user scroll to this cell to perform its content and the question is there similar method to know what cells are out of the screen while scrolling?
Take a look at UITableView's visibleCells method. It'll tell which ones are visible. Then I'm assuming you know how many total cells there are and you can do the logic to work out which ones are not visible.

iOS table view having text fields scrolling issue

I have a UITableView which is composed of custom cells. There are 21 cells containing text fields.
When I try to scroll and edit the cells at the bottom of the UITableView, I can't manage to get my cells properly positioned above the keyboard.
Also i created a custom cell class separately for my cells. So keyboard delegates are available on that class only.
I have seen many answers talking about changing view sizes,etc... but none of them has worked nicely so far.
Could anybody clarify the "right" way to do this with a concrete code example?
There are three approaches to this problem all of which tie into the keyboard did / will show notification....
When the keyboard appears, you move the whole view (including tableview) up to ensure the selected textfield is shown on screen, you can calculate this by getting the keyboard height (from the notification) and the textfield position.
You can resize the tableview so that it is only as big as the portion of the screen not covered by the keyboard, again get the height from the notification.
You can set the content inset for the tableview (for the bottom value) to the height of the keyboard.
If you need to, when resizing or repositioning the tableview, you may need to set it to scroll the content to the rect of the text field to ensure it is in view...
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;