0

enter image description here私はswiftでプロジェクトをやっています。私は、コレクションビューを定義し、そのセルをカスタマイズして、イメージのように見えるようにしました。今、私がセルをクリックすると、私の関数は私がどのセルをクリックしたかを教えてくれるが、もっと詳細な情報を得たいと思う。例えば、セル番号2の画像を得る。その目的のために、私はタップジェスチャーを定義しようとしましたが、それは動作しないようですね?ここにすべての私のコードです:カスタマイズされたセルのどの部分がUICollectionViewでクリックされたかを知るには?

輸入のUIKit 輸入AVFoundation 輸入AVKit

class ViewUSRController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource { 

    @IBOutlet var collectionView: UICollectionView! 
    var cell : MyCollectionViewCell? 

    var names = ["Danial" , "dkfloza" , "Kosarifar" , "IOS" , "Learning", "sina" ] 
    var likes = ["23" , "7" , "17" , "100K" , "1000K" , "100"] 

    var dislikes = ["0","0","0","0","0","0"] 





    var playerViewController = AVPlayerViewController() 
    var playerView = AVPlayer() 


override func viewDidLoad() { 
     super.viewDidLoad() 

     view.backgroundColor = .black 
     self.collectionView.delegate = self 




     let tap = UITapGestureRecognizer(target: self.cell?.myLabel, action:#selector(handleTap)) 
     tap.numberOfTapsRequired = 1 
     cell?.myLabel.addGestureRecognizer(tap) 

    } 

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ 

     return self.names.count 
    } 


    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 
     cell = collectionView.dequeueReusableCell(withReuseIdentifier: "id", for: indexPath as IndexPath) as! MyCollectionViewCell 

     // Use the outlet in our custom class to get a reference to the UILabel in the cell 
     cell?.myLabel?.text = self.names[indexPath.row] 
     cell?.myLabel?.textColor = .black 
     cell?.likes_num?.text = likes[indexPath.row] 
     cell?.dislike_num?.text = dislikes[indexPath.row] 
     cell?.likes_num?.textColor = .black 
     cell?.dislike_num?.textColor = .black 


     return cell! 

    } 






//  
//  
// func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
//  // handle tap events 
//  print("You selected cell #\(indexPath.item)!") 
//   
//   
//   
//   
// } 


    func handleTap(){ 
     print("hello") 
    } 







} 






class MyCollectionViewCell : UICollectionViewCell{ 
    //appClub 
    @IBOutlet var myLabel: UILabel! 
    //imagePlace 
    @IBOutlet var viewTesting: UIView! 
    //likes 
    @IBOutlet var likes_num: UILabel! 
    //dislikes 
    @IBOutlet var dislike_num: UILabel! 











} 
+1

ラベルにボタンを配置し、それらを中央+等幅+等高線として自動レイアウトし、デリゲートまたはクロージャベースのアクションを作成することができます。 – SeanLintern88

+1

これにはベストプラクティスが多すぎます。しかし、それぞれの練習では、あなた自身の 'UITouch'オブジェクトを扱わなければなりません。 'touches'イベントとコレクションビューデリゲートを一緒に使ってみてください。接触しているあなたのアイテム(セル)とコレクションビューのタッチの位置(またはあなたの上のセル)を特定します。その後、そのタッチ位置をタッチしたアイテムの座標系にマッピングして、ビューを取得することができます。 –

+1

あなたの仕事を達成するためにジェスチャーを使用しようとすると、レスポンダ( 'UIResponder'オブジェクト)の階層を変更する可能性があります。これは事態を悪化させます。 –

答えて

0

- >

override func viewDidLoad() { 
     super.viewDidLoad() 

     let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap)) 
     tap.delegate = self 
     tap.numberOfTapsRequired = 1 
     tap.numberOfTouchesRequired = 1 
     tableView.addGestureRecognizer(tap) 
    } 

テーブルビューにジェスチャーを追加 - >をタップ検出

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { 
    let tableView = gestureRecognizer.view as! UITableView 
    let point: CGPoint = gestureRecognizer.location(in: tableView) 
    if (tableView.indexPathForRow(at: point) != nil) { 
     return true; 
    } 
    return false; 
} 

- >セル内の位置を検索し、suにタグを付けるbviewビュー(例: imageView、label)を使用すると、正確なビューを取得することができます。

public func handleTap(tap: UITapGestureRecognizer) 
{ 
    if (UIGestureRecognizerState.ended == tap.state) { 
     let tableView = tap.view as! UITableView 
     let point: CGPoint = tap.location(in: tableView) 
     let indexPath = tableView.indexPathForRow(at: point) 
     tableView.deselectRow(at: indexPath!, animated: false) 
     let cell = tableView.cellForRow(at:indexPath!) 
     let pointInCell = tap.location(in: cell) 
     if ((cell?.imageView?.frame)!.contains(pointInCell)) { 
      // user tapped image 
     } else { 
      // user tapped cell 
     } 
    } 
} 
+0

私はtableViewから与えられたコードをCollectionViewに変換しようとしています。 'if((cell?.imageView?.frame)!. contains(pointInCell))' 'セルの後にあるようです 'というラベルを除いて、セル内にある要素のいずれかを取ることはできません。 imageViewまたは.... タップ検出のためのBTWセレクタは '#selector(self.handleTap(tap :))'でなければなりません。そうでなければあいまいな使い方についてはコンパイルエラーを返します:) –

+0

これで問題は何ですか?ラベルセルにアクセスできませんか?.myLabel?これらのプロパティは、セルクラスでpublicまたはinternalに定義されている場合にアクセスできます。 – Saurav

関連する問題