IGListKit은 UICollectionView를 위한 강력한 프레임워크입니다. 이 프레임워크를 사용하면 커스텀 셀에 스와이프 가능한 액션을 설정할 수 있습니다. 이번 블로그 포스트에서는 IGListKit을 사용하여 셀의 스와이프 가능한 액션을 설정하는 방법을 알아보겠습니다.
IGListKit을 설치합니다.
먼저, IGListKit을 프로젝트에 설치해야 합니다. CocoaPods를 사용하면 아래와 같이 Podfile에 IGListKit을 추가하고 설치할 수 있습니다.
pod 'IGListKit'
셀에 스와이프 가능한 액션을 추가합니다.
IGListKit은 IGListBindingSectionController
클래스를 사용하여 섹션과 각 셀을 관리합니다. 셀에 스와이프 가능한 액션을 추가하려면 다음과 같은 단계를 따르면 됩니다.
IGListBindingSectionController
클래스를 상속받은 새로운 클래스를 생성합니다.
class CustomSectionController: IGListBindingSectionController<CustomModel, CustomCell> {
//...
}
IGListBindingSectionController
클래스의UICollectionViewDelegate
프로토콜 메서드 중 하나인editActionsForItemAtIndexPath
를 오버라이드합니다. 이 메서드는 스와이프 액션을 정의하는 데 사용됩니다.
class CustomSectionController: IGListBindingSectionController<CustomModel, CustomCell> {
override func collectionView(_ collectionView: UICollectionView, editActionsForItemAtIndexPath indexPath: IndexPath) -> [UICollectionViewRowAction]? {
let deleteAction = UICollectionViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
// 삭제하고자 하는 로직을 구현합니다.
}
let editAction = UICollectionViewRowAction(style: .normal, title: "Edit") { (action, indexPath) in
// 편집하고자 하는 로직을 구현합니다.
}
return [deleteAction, editAction]
}
}
-
삭제와 편집 동작을 구현하기 위해
UICollectionViewRowAction
객체를 생성하고editActionsForItemAtIndexPath
메서드에서 반환합니다.style
속성을 통해 동작의 스타일을 설정하고,title
속성을 통해 표시될 텍스트를 설정할 수 있습니다. 클로저 내부에서 해당 동작을 구현합니다. -
프로젝트의 데이터 소스에서 스와이프 가능한 셀을 사용할 섹션 컨트롤러를 등록합니다.
let sectionController = CustomSectionController()
adapter.collectionViewDelegate = sectionController
adapter.collectionViewDataSource = sectionController
위의 단계를 따르면 기존 IGListKit 데이터 소스와 섹션 컨트롤러를 사용하여 셀에 스와이프 가능한 액션을 설정할 수 있습니다.
이제 IGListKit으로 셀의 스와이프 가능한 액션을 설정하는 방법을 알게 되었습니다. IGListKit은 매우 강력한 도구이며 다양한 기능을 제공하므로 프로젝트에 적용해보시기 바랍니다.
참고 자료
- IGListKit: https://github.com/Instagram/IGListKit