diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index 9595c1d..1d6ab47 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -73,9 +73,9 @@ extension ViewController: UICollectionViewDataSource { guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WaterfallViewCell.reuseIdentifier, for: indexPath) as? WaterfallViewCell else { fatalError() } - let titles: [String] = ["Shanghai", "Chongqing", "New York", "San Francisco", "Tokyo", "Phuket", "Singapore", "Wuhan", "Shenzhen", "Los Angeles"] + let cities: [String] = ["Shanghai", "Chongqing", "New York", "San Francisco", "Tokyo", "Phuket", "Singapore", "Wuhan", "Shenzhen", "Los Angeles"] let colors: [UIColor] = [.red, .magenta, .brown, .blue, .purple, .blue, .cyan, .gray, .green, .yellow, .purple] - cell.titleLabel.text = titles.randomElement()! + cell.titleLabel.text = cities.randomElement()! cell.titleLabel.backgroundColor = colors.randomElement()! return cell } @@ -88,6 +88,7 @@ extension ViewController: UICollectionViewDelegateWaterfallLayout { } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { - print("did select item at \(indexPath.item)") + guard let selectedCell = collectionView.cellForItem(at: indexPath) as? WaterfallViewCell else { return } + print("Selected city is \(selectedCell.titleLabel.text ?? "None").") } } diff --git a/README.md b/README.md index 7283a56..cb25848 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,12 @@ A waterfall-like (Pinterest-style) layout for UICollectionView. This library is a modified lightweight Swift version based on [`CHTCollectionViewWaterfallLayout`](https://github.com/chiahsien/CHTCollectionViewWaterfallLayout.git), very easy to integrated with your existing project just like using Apple official `UICollectionViewFlowLayout` and `UICollectionViewDelegateFlowLayout`. -## Requirements -iOS 9.0+ - ## Preview ![Preview](preview.gif) +## Requirements +iOS 9.0+ + ## Installation #### Swift Package Manager (Recommended) @@ -16,7 +16,7 @@ iOS 9.0+ - Add `https://github.com/Jinya/WaterfallLayout.git` - Select "Exact Version" (recommend using the latest exact version) -## Package Content +## Package content Only contains: - `UICollectionViewWaterfallLayout.swift` @@ -24,7 +24,7 @@ Only contains: and a mini example project. -## How to use +## Example codes ```swift import UIKit import WaterfallLayout diff --git a/Sources/WaterfallLayout/UICollectionViewWaterfallLayout.swift b/Sources/WaterfallLayout/UICollectionViewWaterfallLayout.swift index 417d0b3..be33335 100644 --- a/Sources/WaterfallLayout/UICollectionViewWaterfallLayout.swift +++ b/Sources/WaterfallLayout/UICollectionViewWaterfallLayout.swift @@ -87,7 +87,7 @@ public class UICollectionViewWaterfallLayout: UICollectionViewLayout { let spaceColumnCount = CGFloat(columnCount - 1) let columnSpacing = delegate?.collectionView?(collectionView!, layout: self, minimumColumnSpacingFor: section) ?? minimumColumnSpacing let width = collectionViewContentWidth(ofSection: section) - return floor((width - (spaceColumnCount * columnSpacing)) / CGFloat(columnCount)) + return (width - (spaceColumnCount * columnSpacing)) / CGFloat(columnCount) } override public func prepare() {