[swift] Swift IGListKit을 사용한 컬렉션 뷰 셀 이동 처리하기

컬렉션 뷰를 사용하여 아이템을 표시하고 이동할 수 있는 앱을 개발 중이신가요? IGListKit은 콘텐츠를 효율적으로 표시하기 위해 좋은 선택입니다. 이번 포스트에서는 Swift IGListKit을 사용하여 컬렉션 뷰 셀의 이동을 처리하는 방법을 알아보겠습니다.

IGListKit 설치하기

IGListKit은 CocoaPods, Carthage 또는 Swift Package Manager를 통해 설치할 수 있습니다. 가장 간편한 방법은 CocoaPods를 사용하는 것입니다. Podfile에 다음을 추가하고 pod install 명령을 실행하세요.

pod 'IGListKit'

이동 가능한 셀 만들기

이동 가능한 셀을 만들기 위해 IGListKitIGListBindingSectionController를 사용할 수 있습니다. 이 클래스는 셀을 컨트롤러에 바인딩하여 이동 가능한 셀을 만들어주는 역할을 합니다.

class DraggableCell: UICollectionViewCell {
    // 셀의 속성과 UI 요소 정의
}

class DraggableSectionController: IGListBindingSectionController<DraggableItem>, IGListBindingSectionControllerDataSource {
    
    override init() {
        super.init()
        dataSource = self
    }
    
    func sectionController(
        _ sectionController: IGListBindingSectionController<IGListDiffable>,
        viewModelsFor object: Any
    ) -> [ListDiffable] {
        // 셀에 표시될 데이터 모델 반환
        return [object as! ListDiffable]
    }
    
    func sectionController(
        _ sectionController: IGListBindingSectionController<IGListDiffable>,
        cellForViewModel viewModel: Any,
        at index: Int
    ) -> UICollectionViewCell & IGListBindable {
        // 드래그 가능한 셀 인스턴스 반환
        let cell = collectionContext?.dequeueReusableCell(
            of: DraggableCell.self,
            for: self,
            at: index
        )
        return cell!
    }
    
    func sectionController(
        _ sectionController: IGListBindingSectionController<IGListDiffable>,
        didMoveTo newObject: Any,
        at index: Int
    ) {
        // 셀 이동 처리
        // 이동된 셀의 데이터 처리 로직 작성
    }
}

DraggableCell은 이동이 가능한 셀을 나타내며 UICollectionViewCell을 상속받습니다. DraggableSectionControllerIGListBindingSectionController를 상속받아 셀의 데이터 소스를 관리하고, 이동된 셀을 처리하는 로직을 작성합니다.

컬렉션 뷰에 적용하기

DraggableSectionController를 생성하고 컬렉션 뷰에 적용해보겠습니다.

class MyViewController: UIViewController, ListAdapterDataSource {
    
    let collectionView: UICollectionView = {
        // 컬렉션 뷰 생성 및 속성 설정
    }()
    
    lazy var adapter: ListAdapter = {
        return ListAdapter(
            updater: ListAdapterUpdater(),
            viewController: self,
            workingRangeSize: 0
        )
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 컬렉션 뷰 설정
        
        adapter.collectionView = collectionView
        adapter.dataSource = self
    }
    
    // ListAdapterDataSource 프로토콜 구현
    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        // 아이템 목록 반환
    }
    
    func listAdapter(
        _ listAdapter: ListAdapter,
        sectionControllerFor object: Any
    ) -> ListSectionController {
        // 셀을 관리할 섹션 컨트롤러 반환
        return DraggableSectionController()
    }
    
    func emptyView(for listAdapter: ListAdapter) -> UIView? {
        // 비어있을 때의 뷰 반환
        return nil
    }
}

MyViewController 클래스는 UICollectionViewController를 상속받습니다. ListSectionController를 사용하여 컬렉션 뷰의 섹션을 관리하는 방식으로 IGListKit이 동작합니다. DraggableSectionController 인스턴스를 반환하는 listAdapter(_:sectionControllerFor:) 메서드를 구현하여 이동 가능한 셀을 사용할 섹션 컨트롤러를 지정합니다.

결론

이와 같이 IGListKit을 사용하여 컬렉션 뷰 셀의 이동을 처리할 수 있습니다. IGListKit은 유연하고 확장 가능한 프레임워크로, 컬렉션 뷰 관리에 편리한 기능을 제공합니다. IGListKit을 이용하여 컬렉션 뷰를 보다 쉽게 구성하고, 사용자가 아이템을 자유롭게 이동할 수 있는 앱을 구현해 보세요.


참고: