繁体   English   中英

在UICollectionView Swift iOS中临时隐藏单元格

[英]Temporarily Hiding a cell in UICollectionView Swift iOS

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

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”。

如果做过这样的事情的人可以分享他们的方法,我将非常感谢! 谢谢!

-Bryce

您可以执行以下操作:

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

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

我已经做了这样的事情。 集合视图的数据源跟踪一个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]];
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM