简体   繁体   中英

UICollectionView scrolling changes the selected cells

I have a UICollectionView with images as cells content. When selecting the cells, i need to visible the selectionView. (on first tap selection view visible and on next tap selection view hidden). i put the code here

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    if(selectionFlag){
        deleteButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        deleteButton.enabled = true
        addButton.enabled = true
        addButton.setTitle("Add to", forState: .Normal)

        let cell = channelItemCollectionView.cellForItemAtIndexPath(indexPath) as! ChannelItemListCollectionViewCell

        cell.selectionView.alpha = 0.4
        cell.tickButton.frame = CGRect(x: ((UIScreen.mainScreen().bounds.width/3)-2) - 25, y: 3, width: 20, height: 20)


        if imageDataSource[indexPath.row][selectionKey] as! String == "0"
        {
            //selected
            imageDataSource[indexPath.row][selectionKey] = "1"
            cell.selectionView.hidden = false
            selectedArray.append([selectionKey:"1", mediaIdKey:String(indexPath.row)])
            cell.insertSubview(cell.selectionView, aboveSubview: cell.channelItemImageView)
        }
        else
        {
            //deselected
            imageDataSource[indexPath.row][selectionKey] = "0"
            cell.selectionView.hidden = true
            selectedArray.append([selectionKey:"0", mediaIdKey:String(indexPath.row)])
            cell.insertSubview(cell.channelItemImageView, aboveSubview: cell.selectionView)
        }

    }
}

and the cellforItematindex is :

  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ChannelItemListCollectionViewCell.identifier, forIndexPath: indexPath) as! ChannelItemListCollectionViewCell

    collectionView.allowsMultipleSelection = true

    if(selectionFlag){
        if(selectedArray.count > 0)
        {
            for element in selectedArray{
                if element[mediaIdKey] as? String ==  imageDataSource[indexPath.row][mediaIdKey] as? String
                {
                    if element[selectionKey] as! String == "1"
                    {
                       imageDataSource[indexPath.row][selectionKey] = "1"
                        mediaSelected.setValue(element[mediaIdKey]!, forKey: String(indexPath.row))
                    }
                    else{
                        imageDataSource[indexPath.row][selectionKey] = "0"
                        mediaSelected.removeObjectForKey(String(indexPath.row))
                    }
                }
            }
        }
    }
   else{
        cell.selectionView.hidden = true
    }

    if imageDataSource.count > 0
    {

        let imageData =  imageDataSource[indexPath.row][mediaUrlKey] as! NSData
        cell.channelItemImageView.image = UIImage(data: imageData)
    }

    cell.tickButton.frame = CGRect(x: ((UIScreen.mainScreen().bounds.width/3)-2) - 25, y: 3, width: 20, height: 20)
    return cell
}

here the cells are selected and selectedArray stores the indexes, but reloading time the cells are not loaded. Please help me?

As per your current implementation, In cell cellForItemAtIndexPath you should additionally set cell.selectionView.hidden = true/false

if element[selectionKey] as! String == "1"
 {
    cell.selectionView.hidden = false // show selection

    imageDataSource[indexPath.row][selectionKey] = "1"
    mediaSelected.setValue(element[mediaIdKey]!, forKey: String(indexPath.row))
 }
 else{
    cell.selectionView.hidden = true // hide selection
  imageDataSource[indexPath.row][selectionKey] = "0"
  mediaSelected.removeObjectForKey(String(indexPath.row))
  }

调用超prepareForReuse在您单元的prepareForReuse方法方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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