Last active
August 29, 2015 14:22
-
-
Save bmichotte/9cd0f7b9c2b6c1cf6bb6 to your computer and use it in GitHub Desktop.
Motion-Kit UICollectionViewCell
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 MyLayout < MK::Layout | |
def layout | |
super.tap do | |
collection_layout = UICollectionViewFlowLayout.new | |
collection_layout.scrollDirection = UICollectionViewScrollDirectionHorizontal | |
collection_layout.minimumLineSpacing = 0 | |
collection_layout.minimumInteritemSpacing = 0 | |
collection = UICollectionView.alloc.initWithFrame(CGRectZero, collectionViewLayout: collection_layout) | |
add collection, :collection do | |
background_color :clear.uicolor | |
constraints do | |
x.is 10 | |
y.is 40 | |
width.equals(:superview).minus(20) | |
height.equals(:superview).minus(45) | |
end | |
end | |
end | |
end | |
end | |
class MyCell < UICollectionViewCell | |
#def initWithStyle(style, reuseIdentifier: identifier) | |
def initWithFrame(frame) | |
super.tap do | |
@layout = MyCellLayout.new(root: self) | |
@layout.build | |
end | |
end | |
end | |
class MyCellLayout < MK::Layout | |
def layout | |
content_view do | |
background_color :red.uicolor | |
add UIView, :subview do | |
background_color :green.uicolor | |
constraints do | |
size.equals(:superview).times(0.9) | |
center.equals(:superview) | |
end | |
end | |
end | |
end | |
end | |
class MyController < UIViewController | |
KSectionCount = 6 | |
KLinesCount = 10 | |
def viewDidLoad | |
@layout = MyLayout.new(root: self.view) | |
@layout.build | |
@collection = @layout.get(:collection) | |
@collection.registerClass(MyCell, forCellWithReuseIdentifier: 'identifier') | |
@collection.delegate = self | |
@collection.dataSource = self | |
end | |
def numberOfSectionsInCollectionView(collection_view) | |
KSectionCount | |
end | |
def collectionView(collection_view, numberOfItemsInSection: section) | |
KLinesCount | |
end | |
def collectionView(collection_view, cellForItemAtIndexPath: index_path) | |
cell = collection_view.dequeueReusableCellWithReuseIdentifier('identifier', forIndexPath: index_path) | |
#cell.contentView.backgroundColor = [(100...200).to_a.sample, (100...200).to_a.sample, (100...200).to_a.sample].uicolor | |
cell | |
end | |
def collectionView(collectionView, layout: collectionViewLayout, sizeForItemAtIndexPath: indexPath) | |
frame = @collection.frame | |
width = (CGRectGetWidth(frame) / KSectionCount).floor | |
height = (CGRectGetHeight(frame) / KLinesCount).floor | |
row = indexPath.row | |
if row > 0 and (row / (KSectionCount - 1).to_f).floor == row / (KSectionCount - 1).to_f | |
width = CGRectGetWidth(frame) - ((KSectionCount - 1) * width) | |
end | |
if indexPath.section == (KLinesCount - 1) | |
height = CGRectGetHeight(frame) - (height * (KLinesCount - 1)) | |
end | |
[width, height] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AHHH OF course. And that's why it worked for me in a
UITableView