0
컬렉션보기에서 드래그 앤 드롭 기능을 사용하고 있습니다. 제대로 작동하지만 사용자가 다른 위치에 셀을 트랩 할 때 배열의 값을 바꿀 때. 컬렉션보기를 다시로드했습니다.하지만 끌어서 놓기 셀 0 및 1 모음보기는 모든 셀을 다시로드하지 않고 하나의 셀만 다시로드합니다.컬렉션보기가 매번 다시로드되지 않음
내 코드의 문제점을 알 수 없습니다. 다음은 제 코드입니다. 당신은 당신이 endInteractiveMovement()
후 reloadData()를 호출하는 데 필요한 모든 콜렉션 뷰를 다시로드 할 .If
longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(gesture:)))
collection_view.addGestureRecognizer(longPressGesture)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell_images", for: indexPath) as! AddImagesCollectionViewCell
cell.layer.borderWidth=1
if Details.Images[indexPath.item].sourceType == sourceType.URL
{
cell.img_images.downloadedFrom(link: Details.Images[indexPath.item].imageURL!, contentMode: .scaleAspectFill)
}
else
{
cell.img_images.image = Details.Images[indexPath.item].image
}
Details.Images[indexPath.row].sequence="\(indexPath.row + 1)"
return cell
}
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool
{
if indexPath.row == Details.Images.count
{
return false
}
return true
}
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
{
print("Starting Index: \(sourceIndexPath.item)")
print("Ending Index: \(destinationIndexPath.item)")
let tempArray = Details.Images[sourceIndexPath.item]
Details.Images.remove(at: sourceIndexPath.item)
Details.Images.insert(tempArray, at: destinationIndexPath.item)
collectionView.reloadData()
}
@objc func handleLongGesture(gesture: UILongPressGestureRecognizer)
{
switch(gesture.state)
{
case .began:
guard let selectedIndexPath = collection_view.indexPathForItem(at: gesture.location(in: collection_view))
else
{
break
}
if !(selectedIndexPath.row == Details.Images.count)
{
collection_view.beginInteractiveMovementForItem(at: selectedIndexPath)
}
case .changed:
guard let selectedIndexPath = collection_view.indexPathForItem(at: gesture.location(in: collection_view))
else
{
break
}
if !(selectedIndexPath.row == Details.Images.count)
{
collection_view.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
}
case .ended:
collection_view.endInteractiveMovement()
default:
collection_view.cancelInteractiveMovement()
}
}
하지만 시간이 지나면 모든 셀이 다시로드됩니다. –
endInteractiveMovement()에서 호출을 다시로드 한 후 작동하지 않습니다. –
collectionView.reloadData()를 collectionView에 작성하면 시간이 다시로드됩니다. moveItem ... 몇 가지 이유 때문에 재정렬이 취소 된 것으로 생각합니다. collection_view.cancelInteractiveMovement()에서 중단 점을 그것을 확인하고, 취소 후에 reloadData()를 추가하십시오. –