Skip to content

Instantly share code, notes, and snippets.

@bmichotte
Last active August 29, 2015 14:22
Show Gist options
  • Save bmichotte/9cd0f7b9c2b6c1cf6bb6 to your computer and use it in GitHub Desktop.
Save bmichotte/9cd0f7b9c2b6c1cf6bb6 to your computer and use it in GitHub Desktop.
Motion-Kit UICollectionViewCell
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
@bmichotte
Copy link
Author

+-- MyCell(#10f08a80, [[846.0, 648.0], [167.0, 72.0]])     
       |   `-- UIView(#10f08b80, [[0.0, 0.0], [167.0, 72.0]])

@bmichotte
Copy link
Author

https://gist.github.com/bmichotte/9cd0f7b9c2b6c1cf6bb6#file-gistfile1-rb-L26
initWithStyle does not exist for UICollectionViewCell

@colinta
Copy link

colinta commented Jun 1, 2015

AHHH OF course. And that's why it worked for me in a UITableView

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment