繁体   English   中英

UICollectionView在reloadData之后不调用didDeselectItemAtIndexPath

[英]UICollectionView does not call didDeselectItemAtIndexPath after reloadData

我试图通过单击一次使UICollectionViewCell显示一些信息,并通过再次单击使其内容消失。 我想到的第一件事是使用didSelectItemAtIndexPath / didDeselectItemAtIndexPath。 但是,它不起作用,因为在调用方法reloadData()之后,每个“选定”单元的状态都回到“未选定”状态,因此从不调用didDeselectItemAtIndexPath方法。

有什么聪明的方法来解决这个问题? 非常感谢您的提前帮助!

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    // do something
        self.UICollectionView.reloadData()
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    // do something else
}

非常感谢您提供的宝贵帮助! 我按照您的建议添加了一个布尔变量来跟踪单元格是否正在显示某些内容,并单独使用didSelectItemAtIndexPath方法。 现在可以使用了!

我相信BaseZen的方法也行得通,并且更加合法。 但是由于我不想更改太多代码,因此我选择了更方便的代码。 但是都一样谢谢!

您实际上并没有使用选择/取消选择单元格的习惯用法。 错误的树。 相反,您将需要自定义UICollectionViewCell来侦听“ Touch Up Inside”事件,并将每次触摸映射到基础数据模型。 然后,当发生触摸事件时,您将像这样查找值:

@IBAction func userDidTouchMeLittleCustomCell(sender: UIView) {
    if myCustomDataModel[sender.tag].numTapsSoFar == 0 {
        // do the first UI thing, and then:
        myCustomDataModel[sender.tag].numTapsSoFar++
    }
    else {
        // do the second UI thing, and maybe reset the model? Or whatever applies.
    }
}

现在,您需要确保获取touch事件的UIView带有正确的tag ,可以由cellForItemAtIndexPath方法处理。

为了不破坏选择/取消选择习惯用法,您可能希望仅对自定义单元格的子视图具有此行为。

暂无
暂无

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

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