调用reloadData时,didDeselectItemAtIndexPath不起作用

时间:2015-07-31 05:05:35

标签: ios iphone swift ios8

当我尝试选择时,

didDeselectItemAtIndexPath方法没有触发。如果我的代码中有任何设置错误,请告诉我。

如果选择了图像,我试图显示勾选的图标,如果取消选择则会消失图像。

感谢您的帮助。

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
    cell.imageView.image = UIImage(contentsOfFile: self.getMediaFilePath(self.mediaModels[indexPath.row].pathToMedia))
    if self.mediaModels[indexPath.row].isSelected {
        cell.imageTicked.hidden = false
        cell.selected = true
    } else {
        cell.imageTicked.hidden = true
        cell.selected = false
    }
    return cell

}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){

    var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
    cell.backgroundColor = UIColor.magentaColor()
    self.mediaModels[indexPath.row].isSelected  = true
    cell.selected = true
    collectionView.reloadData()
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath){

    var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
    cell.backgroundColor = UIColor.whiteColor()
    self.mediaModels[indexPath.row].isSelected  = false
    cell.selected = false
    collectionView.reloadData()
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    let cellSize = (collectionView.frame.width / 4) - 4

    return CGSizeMake(cellSize, cellSize)
}

2 个答案:

答案 0 :(得分:1)

试试这个

使用cell.userInteractionEnabled = YES;在cellForRowAtIndexPath方法中。

答案 1 :(得分:0)

javascript_tag

我无需重新加载数据即可使用。只需在didSelectItemAtIndexPath和didDeselectItemAtIndexPath方法中设置隐藏的imageTicked。感谢。