简体   繁体   English

UICollectionView滚动更改选定的单元格

[英]UICollectionView scrolling changes the selected cells

I have a UICollectionView with images as cells content. 我有一个UICollectionView带有图像作为单元格内容。 When selecting the cells, i need to visible the selectionView. 选择单元格时,我需要可见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 : 并且cellforItematindex为:

  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. 这里选择了单元,selectedArray存储了索引,但是没有重新加载单元时间。 Please help me? 请帮我?

As per your current implementation, In cell cellForItemAtIndexPath you should additionally set cell.selectionView.hidden = true/false 按照您当前的实现,在单元格cellForItemAtIndexPath您还应该设置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方法方法。

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

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