Skip to content

Instantly share code, notes, and snippets.

let item = provider.items[indexPath.row] as! MyViewModel
let textHeight = item.title.count
let extensionHeight = Double(textHeight) * 0.70
return AdaptiveCollectionConfig.cellBaseHeight + CGFloat(textHeight) + CGFloat(extensionHeight)
let item = provider.items[indexPath.row] as! MyViewModel
let textHeight = item.title.heightWithConstrainedWidth(width: width-24, font: UIFont.systemFont(ofSize: 10))
//Minus 24 points because that is the padding of the edges of the label
let extensionHeight = Double(textHeight) * 0.70
return AdaptiveCollectionConfig.cellBaseHeight + CGFloat(textHeight) + CGFloat(extensionHeight)
extension String {
func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedString.Key.font: font], context: nil)
return boundingBox.height
}
}
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
///1.
var baseCollectionViewArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
var secondCollectionViewArray = [String]()
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == baseCollectionView {
let alphabetLetter = baseCollectionViewArray[indexPath.item]
secondCollectionViewArray.append(alphabetLetter)
let newIndexPath = IndexPath(item: secondCollectionViewArray.count - 1, section: 0)
secondCollectionView.insertItems(at: [newIndexPath])
///First, add a cell to the second collectionview.
baseCollectionViewArray.remove(at: indexPath.item)
baseCollectionView.deleteItems(at: [indexPath])
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == baseCollectionView {
let alphabetLetter = baseCollectionViewArray[indexPath.item]
secondCollectionViewArray.append(alphabetLetter)
let newIndexPath = IndexPath(item: secondCollectionViewArray.count - 1, section: 0)
secondCollectionView.insertItems(at: [newIndexPath])
baseCollectionViewArray.remove(at: indexPath.item)
baseCollectionView.deleteItems(at: [indexPath])
class AlphabetComponent: NSObject {
var labelName = ""
var orderIdentifier = 0
}
class MyViewController : UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
///.......
override func loadView() {
let view = UIView()
view.backgroundColor = .white
///ADD this stuff:
for (index, singleLetter) in baseCollectionViewArray.enumerated() {
let newComponent = AlphabetComponent()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! AlphabetCollectionCell
if collectionView == baseCollectionView {
///We have 2 collectionviews, so we must differentiate.
cell.backgroundColor = .green
cell.nameLabel.text = newBaseCollectionViewArray[indexPath.item].labelName
} else {
cell.backgroundColor = .red
cell.nameLabel.text = secondCollectionViewArray[indexPath.item].labelName
func calculateWhereToPlaceComponent(component: AlphabetComponent, placeInSecondCollectionView: IndexPath) {
let componentOrderID = component.orderIdentifier
var indexPathToAppendTo = 0
for (index, singleComponent) in newBaseCollectionViewArray.enumerated() {
///We are going to check if the singleComponent's order identifier is smaller than componentOrderID.
///If it is smaller, we know we must insert the cell ONE to the right of this indexPath.
if singleComponent.orderIdentifier < componentOrderID {
indexPathToAppendTo = index + 1
}