我试图通过单击一次使 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
}
非常感谢你们提供的宝贵帮助!我听从了您的建议,添加了一个 bool 变量来跟踪单元格是否显示某些内容,并单独使用 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.
}
}
现在,您需要确保获取触摸事件的 UIView 配备了正确的 标签
,这可以通过 cellForItemAtIndexPath
方法处理。
为了不破坏选择/取消选择习惯用法,您可能希望仅对自定义单元格的 subview 具有此行为。
关于ios - UICollectionView 在 reloadData 后不调用 didDeselectItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31779257/