user5934219
user5934219

Reputation:

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?

Upvotes: 0

Views: 897

Answers (2)

Míng
Míng

Reputation: 2608

Call super prepareForReuse method in your cell's prepareForReuse method.

Upvotes: 1

Anil Varghese
Anil Varghese

Reputation: 42977

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

Upvotes: 1

Related Questions