2016-03-23 11 views
3

テーブルビューでは、セルにチェックマークを付けることができます。コレクションビュー(画像)でチェックマークを表示する方法

しかし、コレクションビューでは、セル(画像)を選択すると、どのようにチェックマークを付けることができますか?

私はちょうどセルとイメージのビュー内でイメージビューを撮って、目盛りイメージを入れました。私のコードは以下の通りです。

しかし、動作していません。

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

    // handle tap events 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! customCollectionViewCell 


    if(cell.checkMarkImage.hidden == true) 
    { 
     print("Hidden") 
     cell.checkMarkImage.hidden = false 

    } 
    else 
    { 
     cell.checkMarkImage.hidden = true 
     print("No Hidden") 
    } 
} 
+0

http://stackoverflow.com/questions/18977527/how-do-i-display-the-standard-checkmark-on-a-uicollectionviewcell – Ujjwal

+1

短い答えでは、カスタムセルを作成するか、見つけるインターネット上のサードパーティーのセル –

+0

Ok ありがとう@ sken3r.MI – kishor

答えて

0

@Kishor、paintcodeは、これを行うためのサードパーティのツールです。私もリンクを提供しています。デフォルトではこの機能を持たないので、これを達成するためのカスタム動作を行う必要があります。ありがとう。

+0

しかし、どのようにしてグリッドビューに入れることができますか? チェックマークのみを作成していますか? ありがとう – kishor

1

私はこのコードを持つ2つの主要な問題を参照してください。あなたは、コレクションビューのキャッシュではなく、画面上の1から異なるセルを取得dequeueReusableCellWithReuseIdentifierメソッドを使用

  1. を。 コレクションビューの代わりにcellForItemAtIndexPathメソッドを使用してください。
  2. セルの状態(選択/非選択)をセル自体に保存しようとしました。 UITableView/UICollectionViewで作業するのはよくある間違いですが、この方法はうまくいかないでしょう。その代わりに、コレクションビューがデータソースcellForItemAtIndexPathメソッドを呼び出すたびに、に復元して、別の場所(辞書など)に状態を維持します。
+0

はい私は試みます。 私はcellForItemAtIndexPathメソッドを使用する必要がありますか? ありがとう – kishor

3

//デリゲートメソッドcellForItemAtIndexPath

func collectionView(collectionView: UICollectionView, 
cellForItemAtIndexPath indexPath: NSIndexPath) -> 
UICollectionViewCell 
{ 
    //Get a reference to our storyboard cell 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(
                  "pickSomecell", 
        forIndexPath: indexPath) as! pickSomeGridViewController 

    //Show Images in grid view 
    cell.cellImage.image = self.arrAllOriginalImages[indexPath.row] 
                  as? UIImage   

    //Check Mark toggle. 
    cell.toggleSelected()  

    //return cell. 
    return cell 
} 

そしてpickSomeGridViewControllerにチェックマークの画像が選択されたかを示しています。

class pickSomeGridViewController: UICollectionViewCell{ 
//Outlet of cell image. 
@IBOutlet var cellImage: UIImageView! 

//Outlet of checkMark image. 
@IBOutlet var cellCheckMarkImage: UIImageView! 

//Function for select and deselect checkmark. 
    func toggleSelected() 
    { 
    //If image is selected. 
    if (selected) 
    { 
      //Show check mark image. 
      self.cellCheckMarkImage.hidden = false   
    } 

    else 
    { 
      //Hide check mark image. 
      self.cellCheckMarkImage.hidden = true    
    } 
    } 
} 
1

VAR arrData = NSMutableArrayの() // 1.Makeこの

class CustomModal: NSObject { 
     //Declare bool variable for select and deselect login 
     var is_selected = Bool() 
     //you can declare other variable also 
     var id = Int32() 

} 




// 2. custom array with modal objects 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let arrTemp = NSArray() 
     arrTemp = [1,2,3,4,5,6,7,8,9,10] 
     for i in 0 ..< arrTemp.count{ 
      let eventModal = CustomModal() 
      eventModal.is_selected = false 
      eventModal.id = arrTemp[i] 
      arrData.add(eventModal) 
     } 
     tblView.reloadData() 

    } 

// 2.コレクションビューのデリゲートメソッドのようなモーダルクラスオブジェクトとModalClass.swiftとNSArrayの

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let modal = arrData[indexPath.row] as! CustomModal() 
    modal.is_selected = true 
    self.arrData.replaceObject(at: indexPath.row, with: modal) 

    tblView.reloadData() 
} 

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { 
    let modal = arrData[indexPath.row] as! CustomModal() 
    modal.is_selected = false 
    self.arrData.replaceObject(at: indexPath.row, with: modal) 

    tblView.reloadData() 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! YourCellClass 
    let modal = arrData[indexPath.row] as! CustomModal 
    if modal.is_selected == true{ 
     cell.imgView.image = UIImage(named:"selected_image") 
    }else{ 
     cell.imgView.image = UIImage(named:"deselected_image") 
    } 
} 
関連する問題