Drag and Drop cell between different Collection Views - ios

I have multiple collection views on one screen
In collectionView One i have normal cell like Square
In collectionView two i have group cell e.g same cell but multiple surrounded by a border of Group
I need to Enable Drag and Drop between One Collection View to Another.
I am using swift 3.0.

You can use this one from GitHub:
https://github.com/mmick66/KDDragAndDropCollectionView
And here is a nice guide for it:
http://blog.karmadust.com/drag-and-drop-between-uicollectionviews/

Drag from collectionView2:
Create an independent and drag-able instance of the cell with the same data and place it above the cell to drag
In Collectionview2, set the cell.isHidden = yes. This way the cell is not visible, but its empty space remains.
When the drag-able cell is significant dragged away from the original position, remove the original cell from the collectionView animated.
Drag to collectionView1:
When the drag-able cell is above collectionView1 and is dropped:
insert (animated) a cell to the target position in collectionView1 and set it hidden
move drag-able cell above the inserted (but hidden) cell
set target cell to isHidden = false
remove drag-able cell from superview

Related

How to add a view as a subview of UITableViewCell but on top of all other views

I have a question - how to add a view as a subview of UITableViewCell but on top of all other views?
Here is an example of what I'm trying to achieve:
The image should scroll with the cell - save it's position relatively to scroll view (table view in this case).
If the cell is to small in height then the emojis overlay will be cut by the cell on top of current one, so how can I add this emojis overlay as a subview but at the same time display it on top of every cell? I've tried bunch of variants for now and nothing is working for me.
Just adding as a subview of the current window is not working for me because this overlay should scroll with the cell.
Tried code:
view.layer.zPosition = 5
cell.contentView.insertSubview(view, aboveSubview: cell.contentView)
cell.contentView.bringSubview(toFront: view)
UIApplication.shared.keyWindow?.addSubview(view)
Try to set cliptobouds = false for your cell.contentview and cell itself.
Otherwise if it is single overlay which you want to be on top of every cell then better implement UISrollViewDelegate and position the overlay accordingly.

Make single cell overlap

I have searched but couldnt find an answer.
Is there a way to make only one specific cell overlap other cell and the rest cells be laid out in default layout.
Yes this is possible very easily. You can make the views inside of the cell overlap the other cells. So just add a subview to your UITableViewCell that overlaps the cells bounds. You could for example use auto layout constraints to position them outside the view or you just give the cell a lower height than its subview.
Then you need to make sure, that your that your cells subviews can be displayed outside of its bounds. You need to set clipsToBounds for this:
let cell = tableView.dequeueReusableCell(withIdentifier: "OverlapCell")
cell?.contentView.clipsToBounds = false
cell?.clipsToBounds = false
Also it is important to set the zPosition of your cell in order to make it appear above all other cells.
cell?.layer.zPosition = 10
Hope this helps.

How to keep subview on UITableViewCell

When the user taps the accessory view, the profile image in the UITableViewCell gets darkened by a black view that gets added on as a subview.
Here is what the cell looks like with the black subview:
Here's the issue: When I tap on another cell, the subview gets removed from the first cell and added to the second:
I would like to keep the subview for all cells that have been tapped.
Here is the code in which I handle that functionality:
self!.profileImageBlackView.cornerRadius = cell.followUserImage.frame.height/2
self!.profileImageBlackView.frame = cell.followUserImage.frame
cell.followUserImage.addSubview(self!.profileImageBlackView)
cell.followButton.hidden = false
For some reason, the follow button gets added to both cells, but the "profileImageBlackView" gets moved from cell to cell depending upon which one was activated.
You cannot display the same view in different cells. If you add the view to another cell it's removed from the first one. You must create a separate view for each cell.

Overlapping UITableviewCells

In a tableview one cell overlaps the cell displayed under it when initially presented. When you scroll down until the top cell is not longer visible and then scroll back up the cell are ok again.
I deactivated the separators with this line:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
The second cell has a view that fill it from top to bottom, that was a border radius. When the cells are initially presented the border is covert.
How can i fix the spacing of the tableviewCells when they are initially loaded? Can i set the z index the second cell, so that the second cell is above the first one?
I finally found that the constraint of the the top cells where causing the trouble. I found a workaround by storing the layout constraint in a property and adjusting the constant of the constraint as needed.
For "Can i set the z index the second cell, so that the second cell is above the first one?"
UITableViewCell is subclass of UIView so in table view delegate use this :
secondCell.layer.zPosition = 1;

How to make a UICollectionViewCell span entire row?

Context
I am trying to create something similar to a Table view using UICollectionView.
I am using Xcode7 and storyboarding.
The way I do it is that I drag the collection view across the entire controller view.
And then I drag the entire cell across the row and align it with the right and left boundaries.
Problem
But, when I place a label inside the cell, then it gets displayed correctly only when the device is in a horizontal position.
When the device is vertical, it gets cut off at the left boundary.
Question
How do I ensure that the width of the collection view cell matches that of the container width?
1) Implement the function of cell size and return the collection width:
-(CGSize) collectionView: (UICollectionView*) collectionView layout:(UICollectionViewLayout*) collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*) indexPath {
CGFloat height = 50; //set the wanted height
return CGSizeMake(collectionView.frame.size.width,height);
}
2) Reload the collection when the screen size change (i.e. orientation change).
It is because you are not using autolayout. You can achieve this entirely in storyboard or through code.
Using Storyboard
Add UICollectionView in your UIViewController in storyboard.
Drag UICollectionView to fill up in your UIViewController.
Add Constraints as shown in image.
Drag UICollectionView to fill up in UICollectionView.
Add UILabel (Or whatever you want to have in cell). Also add constraints in that element.
Build and run.

Resources