CoreData의 엔티티를 NSFetchedResultsController와 함께 표시하는 UICollectionView가 있습니다. 스크린 샷에서 볼 수 있듯이 사용자는 테두리가 표시된 여러 셀을 선택할 수 있습니다.UICollectionView + NSFetchedResultsController에서 셀 재정렬
내 모델은 기본적으로 그냥 문자열과 UICollectionView를 통해 선택을 처리하기위한 부울있다.
var url: String
var selected: Bool
var section:Section
나는 UICollectionViewCells
의 재정렬을 지원하기 위해 func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
을 구현했습니다. 드래그 앤 드롭 작업은 정상적으로 작동하지만 이동 된 항목을 CoreData에 저장하는 데 어려움을 겪고 있습니다. 모델에 position
속성을 추가해야합니까?
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
var sourceArray = self.fetchedResultController.fetchedObjects
let item = sourceArray?.remove(at: sourceIndexPath.item)
sourceArray?.insert(item!, at: destinationIndexPath.row)
coreData.saveContext()
}
나는이 속성이 있기 때문에 읽기 전용 wokring되지 않는, 직접
fetchedResultsController.fechtedObjects
을 수정했습니다. 무슨
UICollectionView
및
NSFetchedResultsController
와 & 드롭을 드래그 할 수있는 가장 좋은 방법입니다.
는
편집 : UICollectionView
의
초기 정렬은 Section
모델의 index 속성을 기반으로합니다. 현재 섹션의 정렬 키가 있으며 섹션 내의 항목은 없습니다. 당신이 발견 한 것처럼
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "section.index", ascending: false)]
let resultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath:"section.name", cacheName: nil)
항목을 정렬하여 컬렉션보기에 표시하는 방법은 무엇입니까? –
질문을 업데이트했습니다. 섹션 정렬은 섹션 모델의 속성을 기반으로합니다. – nor0x