This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AlphabetComponent: NSObject { | |
var labelName = "" | |
var orderIdentifier = 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
OlderNewer