【问题标题】:Collection View not reload every time集合视图不会每次都重新加载
【发布时间】:2018-05-29 16:28:25
【问题描述】:

我在集合视图中使用拖放功能。它工作正常,但是当用户将单元格捕获到另一个位置时替换数组的值时。我已经重新加载集合视图。但是当我们拖放单元格 0 和1 个集合视图不会重新加载所有单元格,它只重新加载一个单元格。

我不知道我的代码有什么问题。 下面是我的代码。

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()
        }
    }

【问题讨论】:

    标签: ios swift drag-and-drop uicollectionview


    【解决方案1】:

    我有同样的问题。不知道为什么在endInteractiveMovement()cancelInteractiveMovement() 之后reloadData() 不起作用,但我有一个解决方法。

    不要使用reloadData(),而是使用reloadSections(),比如:

    self.collectionView.cancelInteractiveMovement()
    
    // Not working: self.collectionView.reloadData()
    UIView.performWithoutAnimation {
        self.collectionView.reloadSections(IndexSet(integer: 0))
    }
    

    另外,如果你在DispatchQueue.main.asyncAfter() 中使用reloadData(),它也会起作用。

    【讨论】:

    • 你是在哪种方法上实现的?
    • 在我调用cancelInteractiveMovement/endInteractiveMovement的方法中。检查我发布的代码片段。
    【解决方案2】:

    您的代码中没有错误,collectionView 只会重新加载一个单元格(您已拖动的一个单元格)。如果您想重新加载所有集合视图,您需要在endInteractiveMovement() 之后调用 reloadData()。

    【讨论】:

    • 但有时它会重新加载所有单元格。
    • 在 endInteractiveMovement() 中重新加载调用后无法正常工作
    • 它重新加载了一段时间,因为你在 collectionView:moveItem 中写了 collectionView.reloadData() ...我想你的重新排序由于某些原因被取消了在 collection_view.cancelInteractiveMovement() 上设置一个断点来检查它,也取消后添加reloadData()。
    【解决方案3】:

    我在通过拖放重新排序时遇到了这个问题 - 集合视图不会重新加载由于移动操作而位置发生变化的所有单元格。我通过在performDropWithCoordinator 的实现中调用moveItemAtIndexPath:toIndexPath: 来修复它。见this question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      相关资源
      最近更新 更多