LoginSignup
5
4

More than 5 years have passed since last update.

[Swift]Sapporoの使い方

Last updated at Posted at 2016-08-30

Sapporoとは

Sapporoとは,iOSのUICollectionViewをCellmodel-drivenで扱うためのマネージャー

環境

  • OSX El Captan
  • Xcode 7.3
  • iOS 8.0
  • Swift 2.2
  • Sapporo 1.1

ViewControllerの実装

ViewController.swift
import UIKit
import Sapporo

class ViewController: UIViewController {

    @IBOutlet var collectionView: UICollectionView!
    lazy var sapporo: Sapporo = { [unowned self] in
        return Sapporo(collectionView: self.collectionView)
        }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // 以下のように,repeatedValue にクラスを渡すと実体は1つのため状態が共有されてしまうので,実際に使うときは注意
        let cellmodels: [MyCellModel] = Array(count: 4,  repeatedValue: MyCellModel())

        let section = SASection()
        section.inset = UIEdgeInsets(top: 30, left: 10, bottom: 10, right: 10)
        section.minimumLineSpacing = 10

        sapporo
            .registerCellByNib(MyCell)
            .setLayout(SAFlowLayout())

        sapporo
            .reset(section)
            .bump()

        section
            .reset(cellmodels)
            .bump()
    }

}

CellとCellModelの実装

MyCell.swift
import UIKit
import Sapporo

class MyCellModel: SACellModel {

    init(){
        let size = CGSize(width: 100, height: 100)
        super.init(cellType: MyCell.self, size: size, selectionHandler: nil)
    }
}

class MyCell: SACell, SACellType {
    typealias CellModel = MyCellModel

    override func configure() {
        super.configure()

    }
}

セルの操作

  • 追加

    section
        .append(cellmodel)
        .bump()
    
    sapporo[1]
        .append(cellmodel)
        .bump()
    
  • 挿入

    section
        .insert(cellmodel)
        .bump()
    
    section
        .insertBeforeLast(cellmodels)
        .bump()
    
  • リセット

    section
        .reset(cellmodels)              
        .bump()
    
    section
        .reset()                            
        .bump()
    
  • 移動

    section
        .move(fromIndex: 5, toIndex: 1)
        .bump()
    
  • 削除

    section
        .remove(1)
        .bump()
    
    section
        .remove(cellmodel)
        .bump()
    
    section
        .remove(2...5)
        .bump()
    
    section
        .removeLast()
        .bump()
    
  • 更新

    let cellmodel = section[1]
    cellmodel.property = newData
    cellmodel.bump()
    

セクションの操作

  • 挿入

    sapporo
        .insert(section, atIndex: 1)
        .bump()
    
  • 移動

    sapporo
        .move(fromIndex: 1, toIndex: 5)
        .bump()
    
5
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4