【问题标题】:Temporarily Hiding a cell in UICollectionView Swift iOS在 UICollectionView Swift iOS 中临时隐藏一个单元格
【发布时间】:2016-01-26 00:31:29
【问题描述】:

我已经尝试了几个小时没有运气。我有一个 UICollectionView 集合视图。集合视图基本上是一个列表,最后一个单元格始终是带有大加号的单元格以添加另一个项目。我已经启用了以下重新排序。我想要它做的是当我开始交互式移动时,加号单元格消失,然后当用户完成编辑时,它再次出现。这是我拥有的代码的基本版本:

func handleLongGesture(gesture: UILongPressGestureRecognizer) {

        switch(gesture.state) {

        case UIGestureRecognizerState.Began:

            ...

            self.collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)

            removeAddCell()

        case UIGestureRecognizerState.Changed:


        case UIGestureRecognizerState.Ended:

            ...


            collectionView.endInteractiveMovement()
            replaceAddCell()

        default:

            collectionView.cancelInteractiveMovement()
        }
    }


    func removeAddCell(){

        print("Reloading data - removing add cell")

        data_source.popLast()

        self.collectionView.reloadData()

    }

    func replaceAddCell(){

        print("Reloading data - replacing add cell")

        data_source.append("ADD BUTTON")

        self.collectionView.reloadData()

    }

这是非常粗糙的伪代码,但我什至无法让最简单的版本工作。使用我拥有的代码,它在我从数据源中删除项目后引用 UICollectionViewCell 的行上给出了可怕的“致命错误:在展开可选值时意外发现 nil”。

如果有做过类似事情的人可以分享他们的方法,我将不胜感激!谢谢!

-布莱斯

【问题讨论】:

  • 与其试图删除它,为什么不使用不透明度(alpha)隐藏它并关闭用户交互?加号变暗而不是完全消失可能是更好的用户体验。如果您开始拖动某个东西,而另一个东西消失了,那可能看起来好像出了点问题。
  • 能用告诉我们哪一行有致命错误吗?
  • 我尝试使用 alpha 0 隐藏它,但单元格仍然存在,因此如果用户将单元格从顶部移动到底部,则在倒数第二个位置添加细胞群岛@迈克尔

标签: ios swift uicollectionview uicollectionviewcell


【解决方案1】:

你可以这样做:

func longPressed(sender: UILongPressGestureRecognizer) {
    let indexPath = NSIndexPath(forItem: items.count - 1, inSection: 0)
    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! YourCollectionViewCell
    switch sender.state {
    case .Began:
        UIView.animateWithDuration(0.3, animations: {
            cell.contentView.alpha = 0
        })

    case .Ended:
        UIView.animateWithDuration(0.3, animations: {
            cell.contentView.alpha = 1
        })

    default: break
    }
}

这样它会逐渐消失而不是突然消失。

【讨论】:

    【解决方案2】:

    我做过类似的事情。集合视图的数据源跟踪 BOOL 以确定是否显示添加项目单元格。并调用 insertItemsAtIndexPaths:deleteItemsAtIndexPaths: 以动画添加项目单元格的出现和消失。我实际上使用编辑按钮来切换模式。但是您可以修改此代码以使用您的手势识别器。

    基本代码:

    self.editing = !self.editing; // toggle editing mode, BOOL that collection view data source uses
    NSIndexPath *indexPath = [self indexPathForAddItemCell];
    if (!self.editing) { // editing mode over, show add item cell
        if (indexPath) {
            [self.collectionView insertItemsAtIndexPaths:@[indexPath]];
        }
    }
    else { // editing mode started, delete add item cell
        if (indexPath) {
            [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多