2016-04-04 17 views
0

私はセルのコンテンツとして画像を持つUICollectionViewを持っています。セルを選択するとき、私はselectViewを見る必要があります。 (最初のタップ選択ビューが表示され、次のタップ選択ビューが非表示になります)。私はここUICollectionViewスクロールで選択したセルが変更されます

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

    } 
} 

コードを入れて、cellforItematindexは次のとおりです。ここでは、細胞が選択されているとselectedArray店インデックスが、リロード時間は、細胞がロードされません

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 
} 

。私を助けてください?あなたの現在の実装を1として

+0

あなたが選択したセルを強調表示する必要があることを意味しますか?あなたはそれを別の見方で示していますか? – Ajumal

答えて

1

は、セルでは、さらに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)) 
    } 
1

コールあなたのセルのprepareForReuse方法でスーパーprepareForReuse方法を設定する必要がありますcellForItemAtIndexPath

関連する問題