A collection view which can add item、delete item dynamically.
- Add Item、delete item dynamically and animatedly.
- Complete customizable cell.
- Complete customizable data structure bind to cell.
-
Define data structure.
struct Model { var text: String var subtext: String init(text: String, subtext: String) { self.text = text self.subtext = subtext } }
-
Create subclass of
DynamicCollectionViewCell
and appoint the data type.class MyCollectionViewCell: DynamicCollectionViewCell<Model> { ... }
-
Create
DynamicCollectionView
and register cell typedynamicCollectionView = DynamicCollectionView<Model>(frame: CGRect(x: 0, y: 20, width: CGRectGetWidth(view.frame), height: 600)) dynamicCollectionView.setBgColor(UIColor.lightGrayColor()) dynamicCollectionView.collectionViewLayout.itemSize = CGSize(width: CGRectGetWidth(dynamicCollectionView.frame), height: 100) dynamicCollectionView.registerCollectionViewCellClass(MyCollectionViewCell.self) view.addSubview(dynamicCollectionView)
-
Add or delete item.
let model = Model(text: "text", subtext: "\(arc4random() % 100)") dynamicCollectionView.addItem(model)
dynamicCollectionView.removeLastItem()
dynamicCollectionView.removeAllItems()